基础知识,知识点总括

一.类机动属性

1.类机关属性

一.类活动属性

一自动属性

亚洲必赢官网 1亚洲必赢官网 2

亚洲必赢官网 3亚洲必赢官网 4

亚洲必赢官网 5亚洲必赢官网 6

 public class Person
  2     {
  3         //自动属性
  4        public string Name { get; set; }
  5 
  6         private int _age;
  7 
  8         public int age {
  9             get { return _age; }
 10             set { _age = value; }
 11         }
 12     }
Name是一个总动属性,是微软自动帮我们生成了字段,_age 时我们自己添加的属性跟Name是一样的效果


2.隐式类型(var)

 static void Var()
  2         {
  3             var person = new Person();
  4 
  5             var p = person;
  6         }

  
  1 public class Person
  2     {
  3         //自动属性
  4        public string Name { get; set; }
  5 
  6         private int _age;
  7 
  8         public int age {
  9             get { return _age; }
 10             set { _age = value; }
 11         }
 12     }
  1 public class Person
  2     {
  3         //自动属性
  4        public string Name { get; set; }
  5 
  6         private int _age;
  7 
  8         public int age {
  9             get { return _age; }
 10             set { _age = value; }
 11         }
 12     }
  1 public class Person
  2     {
  3         //自动属性
  4        public string Name { get; set; }
  5 
  6         private int _age;
  7 
  8         public int age {
  9             get { return _age; }
 10             set { _age = value; }
 11         }
 12     }

一.var类型会被编写翻译器依照早先值的类型自动测算出切实的门类。

View Code

View Code

View Code

  1. var类型无法做参数。

Name为类的自发性属性,其实跟上面age的定义方法是1律的,只可是是微软机动帮您生成了字段。

Name为类的活动属性,其实跟上边age的定义方法是如出壹辙的,只但是是微软机关帮你生成了字段。

Name为类的机关属性,其实跟上边age的定义方法是壹律的,只然而是微软机动帮您生成了字段。

亚洲必赢官网 ,3.无法将null赋值给var类型。

基础知识,知识点总括。贰.隐式类型

贰.隐式类型

二.隐式类型

四.语句中只声惠氏次变量,申明后无法改变项目。

亚洲必赢官网 7亚洲必赢官网 8

亚洲必赢官网 9亚洲必赢官网 10

亚洲必赢官网 11亚洲必赢官网 12

3匿名类

  1         static void W02AnmClass()
  2         {
  3             var d = new { name = "张三", age = 13 };
  4             var d2 = new { name = 12, age = "13" };
  5             var d3 = new { name = "张三", age = "13", gender="男"};
  6         }
  1         static void Var()
  2         {
  3             var person = new Person();
  4 
  5             var p = person;
  6         }
  1         static void Var()
  2         {
  3             var person = new Person();
  4 
  5             var p = person;
  6         }
1         static void Var()
  2         {
  3             var person = new Person();
  4 
  5             var p = person;
  6         }

壹.无名类本质上是编写翻译器生成的2个泛型类。

View Code

View Code

View Code

2.当无名类的参数个数同样的时候是共用3个泛型类。

一.var类型会被编写翻译器依照早先值的种类自动测算出实际的连串。

一.var类型会被编写翻译器依照早先值的门类自动测算出实际的项目。

1.var类型会被编写翻译器依照开首值的档期的顺序自动测算出实际的连串。

3.当无名氏类的参数个数不均等的时候是独家生成不一致的泛型类。

  1. var类型不能够做参数。
  1. var类型不可能做参数。
  1. var类型不可能做参数。

肆.无名类初步化的时候自然要赋值。

3.无法将null赋值给var类型。

3.无法将null赋值给var类型。

3.无法将null赋值给var类型。

 

四.语句中只声美素佳儿次变量,表明后不可能改换项目。

4.语句中只声Bellamy(Bellamy)次变量,注解后不可能退换项目。

4.语句中只声爱他美次变量,评释后无法改动项目。

四.暗中认可值和命名参数

三、匿名类

三、匿名类

三、匿名类

 1  static void W03DefaultValue()
  2         {
  3             W0301();
  4             W0301("李四");
  5             W0301(age: 20);
  6         }
  7         static void W0301(string name="张三",int age=18)
  8         {
  9             Person person = new Person();
 10             person.Name = name;
 11             person.Age = age;
 12         }

亚洲必赢官网 13亚洲必赢官网 14

亚洲必赢官网 15亚洲必赢官网 16

亚洲必赢官网 17亚洲必赢官网 18

一.当三个艺术里面包车型客车参数有暗中认可值的时候,能够一向调用方法毫无传值,编写翻译器会自行调用参数的值。

  1         static void W02AnmClass()
  2         {
  3             var d = new { name = "张三", age = 13 };
  4             var d2 = new { name = 12, age = "13" };
  5             var d3 = new { name = "张三", age = "13", gender="男"};
  6         }
  1         static void W02AnmClass()
  2         {
  3             var d = new { name = "张三", age = 13 };
  4             var d2 = new { name = 12, age = "13" };
  5             var d3 = new { name = "张三", age = "13", gender="男"};
  6         }
  1         static void W02AnmClass()
  2         {
  3             var d = new { name = "张三", age = 13 };
  4             var d2 = new { name = 12, age = "13" };
  5             var d3 = new { name = "张三", age = "13", gender="男"};
  6         }

贰.当措施有暗中认可值时,倘若调用的时候只传入第四个参数,那么编写翻译器会把首个参数设置为暗中认可值。

View Code

View Code

View Code

三.倘使急需给某1个参数赋值,书写的格式是 参数名:值。

一.佚名类本质上是编写翻译器生成的二个泛型类。

一.无名类本质上是编写翻译器生成的1个泛型类。

一.佚名类本质上是编写翻译器生成的叁个泛型类。

 

2.当佚名类的参数个数一样的时候是公私二个泛型类。

二.当佚名类的参数个数同样的时候是公共2个泛型类。

二.当无名氏类的参数个数同样的时候是公私叁个泛型类。

5目的,集结初阶化器

三.当佚名类的参数个数不一致的时候是分别生成差异的泛型类。

叁.当无名类的参数个数不等同的时候是分别生成差别的泛型类。

三.当无名类的参数个数不均等的时候是分别生成分裂的泛型类。

       static void W0401Object()
  2         {
  3             Person person = new Person() {
  4                 Name="张三",
  5                 Age=23
  6             };
  7         }
  8         static void W0402Object()
  9         {
 10             var personlist = new List<Person>() {
 11                 new Person() {Name="张三",Age=22 },
 12                 new Person() {Name="李四",Age=12 }
 13             };
 14         }
 15         static void W0403Object()
 16         {
 17             Person[] arrperson = new Person[] {
 18                 new Person() {Name="张三",Age=22 }
 19             };
 20         }

4.佚名类起头化的时候一定要赋值。

四.佚名类发轫化的时候势要求赋值。

④.佚名类开头化的时候一定要赋值。

一.对象/集合初叶化器,能够在new一个目的只怕聚众的时候,直接在前面加{}里面对品质间接赋值恐怕直接new三个目的给集合。

四、暗中认可值和命名参数

四、默许值和命名参数

4、暗许值和命名参数

5无名艺术(佚名函数、无名委托)

亚洲必赢官网 19亚洲必赢官网 20

亚洲必赢官网 21亚洲必赢官网 22

亚洲必赢官网 23亚洲必赢官网 24

 1        static bool Process(int p)
  2         {
  3             return p > 2;
  4         }
  5 
  6         static void W0401()
  7         {
  8             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  9             //直接传入一个方法名称也可以不过这个方法名称的参数以及返回值类型要跟
 10             //public delegate bool Predicate<in T>(T obj);一样
 11             var nlist =list.FindAll(Process);
 12             foreach (var item in nlist)
 13             {
 14                 Console.WriteLine(item);
 15             }
 16 
 17             Console.WriteLine("=================");
 18             //匿名方法的写法:delegate(编写匿名方法的参数,参数有程序员自己定义){函数的方法体代码}
 19             var nlist2 = list.FindAll(delegate (int p) { return p > 3; });
 20             foreach (var item in nlist2)
 21             {
 22                 Console.WriteLine(item);
 23             }
 24         }
  1  static void W03DefaultValue()
  2         {
  3             W0301();
  4             W0301("李四");
  5             W0301(age: 20);
  6         }
  7         static void W0301(string name="张三",int age=18)
  8         {
  9             Person person = new Person();
 10             person.Name = name;
 11             person.Age = age;
 12         }
  1  static void W03DefaultValue()
  2         {
  3             W0301();
  4             W0301("李四");
  5             W0301(age: 20);
  6         }
  7         static void W0301(string name="张三",int age=18)
  8         {
  9             Person person = new Person();
 10             person.Name = name;
 11             person.Age = age;
 12         }
 1  static void W03DefaultValue()
  2         {
  3             W0301();
  4             W0301("李四");
  5             W0301(age: 20);
  6         }
  7         static void W0301(string name="张三",int age=18)
  8         {
  9             Person person = new Person();
 10             person.Name = name;
 11             person.Age = age;
 12         }

陆恢宏方法

View Code

View Code

View Code

/// 1.扩展方法必须是一个静态方法
  3         /// 2.静态方法必须放在静态类中
  4         /// 3.扩展方法第一个参数必须是this开头,并且指定扩展方法是哪个类型上的
  5         /// 4.扩展方法只能有指定的类型才能点出来
  6         /// 5.扩展方法的this后面的参数不属于 方法的参数
  7         /// 6.如果扩展方法跟实例方法同名,先调用实例方法
  8         /// 7.子类可以调用父类的扩展方法
  9         /// 8.接口上的扩展方法可以被实现类的对象直接使用
 10         /// 9.扩展方法的本质,最终还是被编译器编译成了 静态类.静态方法()

  静态方法========

 12         static void W0601()
 13         {
 14             DateTime dt = DateTime.Now;
 15             string fmtstr= dt.FmtDate();
 16         }

一.当一个主意里面包车型客车参数有暗中同意值的时候,能够平素调用方法毫无传值,编写翻译器会活动调用参数的值。

一.当3个主意里面包车型客车参数有默许值的时候,能够直接调用方法毫无传值,编译器会活动调用参数的值。

一.当1个措施里面包车型客车参数有暗许值的时候,能够直接调用方法毫无传值,编译器会自行调用参数的值。

public static class ExtHelper
{
public static string FmtDate(this DateTime dt)
{
return dt.ToString(“YYYY-MM-DD HH:MM:ss”);
}
public static string FmtDate(this DateTime dt,string str)
{
return dt.ToString(“YYYY-MM-DD HH:MM:ss”+str);
}
}

贰.当措施有暗中认可值时,若是调用的时候只传入第2个参数,那么编写翻译器会把第三个参数设置为私下认可值。

贰.当主意有私下认可值时,借使调用的时候只传入第一个参数,那么编写翻译器会把第壹个参数设置为暗许值。

二.当办法有暗中认可值时,假如调用的时候只传入第三个参数,那么编写翻译器会把第三个参数设置为暗中同意值。

 

3.只要急需给某1个参数赋值,书写的格式是 参数名:值。

三.假如急需给某三个参数赋值,书写的格式是 参数名:值。

3.只要要求给某一个参数赋值,书写的格式是 参数名:值。

⑦连串内置的委托

伍、对象/群集初阶化器

伍、对象/会集初叶化器

5、对象/集合起始化器

#region 系统内置委托
  2         #region Action委托 接收参数 无法回值
  3         static void W0701Action()
  4         {
  5             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
  6 
  7             list.ForEach(i => Console.WriteLine(i));
  8         }
  9         #endregion
 10 
 11         #region Predicate 泛型委托:接收一个参数,返回bool类型
 12         static void W0702Predicate()
 13         {
 14             var list = new List<Person>(){
 15                 new Person { Name = "张三", Age = 12 },
 16                 new Person { Name = "李四", Age = 32 },
 17                 new Person { Name = "王五", Age = 44 }
 18             };
 19 
 20             var nlist = list.FindAll(c => c.Age > 22);
 21             //获取当前list集合中,名字为张三的对象,如果有多个只返回第一个
 22 
 23             var person = list.Find(c => c.Name == "张三");
 24         }
 25         #endregion
 26 
 27         #region Func:接收参数,返回参数,但是不固定
 28         static void W0703Func()
 29         {
 30             var list = new List<Person>(){
 31                 new Person { Name = "张三", Age = 12 },
 32                 new Person { Name = "李四", Age = 32 },
 33                 new Person { Name = "王五", Age = 44 }
 34             };
 35 
 36             var nlist = list.Where(c => c.Age > 1);
 37             var nlist1 = nlist.ToList();
 38             nlist1.ForEach(c => Console.WriteLine(c.Name.ToString()));
 39         }
 40         #endregion
 41 
 42         #region System.Comparison<T> 返回一个整数,接收两个同类型的参数
 43         static void W0704Comparison()
 44         {
 45             var list = new List<Person>(){
 46                 new Person { Name = "张三", Age = 12 },
 47                 new Person { Name = "李四", Age = 32 },
 48                 new Person { Name = "王五", Age = 44 }
 49             };
 50 
 51             list.Sort((p, n) =>p.Age - n.Age);
 52             list.ForEach(c=>Console.WriteLine(c.Name.ToString()+":"+c.Age.ToString()));
 53         }
 54 
 55         #endregion
 56         #endregion

亚洲必赢官网 25亚洲必赢官网 26

亚洲必赢官网 27亚洲必赢官网 28

亚洲必赢官网 29亚洲必赢官网 30

捌.SQO形式 -规范查询运算符

  1  static void W0401Object()
  2         {
  3             Person person = new Person() {
  4                 Name="张三",
  5                 Age=23
  6             };
  7         }
  8         static void W0402Object()
  9         {
 10             var personlist = new List<Person>() {
 11                 new Person() {Name="张三",Age=22 },
 12                 new Person() {Name="李四",Age=12 }
 13             };
 14         }
 15         static void W0403Object()
 16         {
 17             Person[] arrperson = new Person[] {
 18                 new Person() {Name="张三",Age=22 }
 19             };
 20         }
  1  static void W0401Object()
  2         {
  3             Person person = new Person() {
  4                 Name="张三",
  5                 Age=23
  6             };
  7         }
  8         static void W0402Object()
  9         {
 10             var personlist = new List<Person>() {
 11                 new Person() {Name="张三",Age=22 },
 12                 new Person() {Name="李四",Age=12 }
 13             };
 14         }
 15         static void W0403Object()
 16         {
 17             Person[] arrperson = new Person[] {
 18                 new Person() {Name="张三",Age=22 }
 19             };
 20         }
 1  static void W0401Object()
  2         {
  3             Person person = new Person() {
  4                 Name="张三",
  5                 Age=23
  6             };
  7         }
  8         static void W0402Object()
  9         {
 10             var personlist = new List<Person>() {
 11                 new Person() {Name="张三",Age=22 },
 12                 new Person() {Name="李四",Age=12 }
 13             };
 14         }
 15         static void W0403Object()
 16         {
 17             Person[] arrperson = new Person[] {
 18                 new Person() {Name="张三",Age=22 }
 19             };
 20         }

创办多少个类

View Code

View Code

View Code

 1  public class Person
  2     {
  3         public int TypeID { get; set; }
  4 
  5         public int ID { get; set; }
  6         //自动属性
  7         public string Name { get; set; }
  8 
  9         private int _age;
 10 
 11         public int Age {
 12             get { return _age; }
 13             set { _age = value; }
 14         }
 15 
 16         public override string ToString()
 17         {
 18             return "ID=" + this.ID + "名称=" + this.Name + ",年龄=" + this.Age + "所属类别=" + this.TypeID;
 19         }
 20     }
 21 
 22     public class PsersonType
 23     {
 24         public int TypeID { get; set; }
 25         public string TName { get; set; }
 26     }

一.目的/群集起始化器,可以在new1个对象或然聚众的时候,直接在后头加{}里面对质量直接赋值或许直接new2个目的给集合。

一.对象/集结开始化器,能够在new多少个指标大概聚众的时候,直接在前边加{}里面对质量直接赋值大概间接new2个目标给集结。

1.对象/会集伊始化器,能够在new1个目的大概聚众的时候,直接在末端加{}里面对性能直接赋值或然直接new三个对象给集结。

SQO常用方法的采取 查询、排序、关联、分组等

陆、匿有名的模特式(无名函数、无名委托)

6、无名氏方式(佚名函数、佚名委托)

陆、无名格局(无名氏函数、无名氏委托)

  1         #region SQO方法 -标准查询运算符
  2         #region 1 where() firstordefult() LastOrDefault方法进行查找操作 (链式编程)
  3         static void W0801()
  4         {
  5             var list = new List<Person>(){
  6                 new Person { Name = "张三", Age = 12 },
  7                 new Person { Name = "李四", Age = 32 },
  8                 new Person { Name = "王五", Age = 44 }
  9             };
 10 
 11             //利用where查找出list中年龄大于22或者名称叫王五
 12             //链式编程
 13             list.Where(c => c.Age > 5 || c.Name == "王五").ToList().ForEach(c => Console.WriteLine(c.ToString()));
 14 
 15             //firstordefult()的演示
 16            var person= list.FirstOrDefault(c => c.Age > 5 || c.Name == "王五");
 17             Console.WriteLine(person.ToString());
 18 
 19             //获取集合中满足条件的最后一个元素
 20             var person2 = list.LastOrDefault(c => c.Age > 5 || c.Name == "王五");
 21             Console.WriteLine(person.ToString());
 22 
 23         }
 24         #endregion
 25 
 26         #region 2 对集合进行排序(正序和倒序)
 27         static void W0802()
 28         {
 29             var list = new List<Person>(){
 30                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 31                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 32                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 33             };
 34             //正序
 35             list.OrderBy(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 36             Console.WriteLine("---------");
 37             //倒序
 38             list.OrderByDescending(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 39         }
 40         #endregion
 41 
 42         #region 3 根据多个字段的组合排序
 43         static void W0803()
 44         {
 45             var list = new List<Person>(){
 46                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 47                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 48                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 49             };
 50             //多个条件进行正序 先按照年龄然后按照ID正序
 51             list.OrderBy(c => c.Age).ThenBy(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 52 
 53             //多条件倒序
 54             list.OrderByDescending(c => c.Age).ThenByDescending(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 55         }
 56         #endregion
 57 
 58         #region 4 投影方法(Select) ( 重点 )
 59         static void W0804()
 60         {
 61             var list = new List<Person>(){
 62                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 63                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 64                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 65             };
 66             //取出list集合里面的部分字段
 67             List<string> nlist = list.Select(c => c.Name).ToList();
 68 
 69             //重新创建一个新的list集合只包含name和age
 70             list.Select(c => new { N = c.Name, A = c.Age }).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 71         }
 72         #endregion
 73 
 74         #region 5 分页方法 Skip(skipNum).Take(页容量)  Skip():表示跳过集合中的前面多少行
 75         static void W0805()
 76         {
 77             int rowcount = 0;
 78             W0805ByPage(1, 2, out rowcount).ForEach(c => Console.WriteLine(c.ToString()));
 79         }
 80         static List<Person> W0805ByPage(int pageindex, int pagesize, out int rowcount)
 81         {
 82             var list = new List<Person>(){
 83                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 84                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 85                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 86             };
 87            // 给rowcount赋予满足条件的数据总行数
 88             rowcount = list.Count();
 89             //经过第几页跳过跳过多少条
 90             int skipCount = (pageindex - 1) * pagesize;
 91             return list.Where(c => c.Age > 1).Skip(skipCount).Take(pagesize).ToList();
 92         }
 93         #endregion
 94 
 95         #region 6 连表查询Join
 96         static void W0806()
 97         {
 98             var list = new List<Person>(){
 99                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
100                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
101                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
102                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
103             };
104 
105             var typelist = new List<PsersonType>()
106             {
107                 new PsersonType(){TypeID=1,TName="老师"},
108                     new PsersonType(){TypeID=3,TName="学生"},
109                     new PsersonType(){ TypeID=2, TName="保安"}
110             };
111 
112             //利用SQO方法Join() 来实现上述业务逻辑
113             var nlist = list.Join(typelist, p => p.TypeID, c => c.TypeID, (p, c) =>new { p.Name,p.Age,c.TName }).ToList();
114 
115             nlist.ForEach(c => Console.WriteLine(c.ToString()));
116         }
117         #endregion
118 
119         #region 7 分组 GroupBy()
120         static void W0807()
121         {
122             var list = new List<Person>(){
123                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
124                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
125                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
126                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
127             };
128 
129             //利用SQO方法进行分组操作
130             list.GroupBy(c => c.TypeID).ToList().ForEach(c => {
131                 Console.WriteLine("----分组号{0}" + c.Key,c.ToList().Count());
132                 c.ToList().ForEach(g => Console.WriteLine(g.ToString()));
133             });
134         }
135         #endregion
136         #endregion

亚洲必赢官网 31亚洲必赢官网 32

亚洲必赢官网 33亚洲必赢官网 34

亚洲必赢官网 35亚洲必赢官网 36

玖.linq的常用方法

  1        static bool Process(int p)
  2         {
  3             return p > 2;
  4         }
  5 
  6         static void W0401()
  7         {
  8             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  9             //直接传入一个方法名称也可以不过这个方法名称的参数以及返回值类型要跟
 10             //public delegate bool Predicate<in T>(T obj);一样
 11             var nlist =list.FindAll(Process);
 12             foreach (var item in nlist)
 13             {
 14                 Console.WriteLine(item);
 15             }
 16 
 17             Console.WriteLine("=================");
 18             //匿名方法的写法:delegate(编写匿名方法的参数,参数有程序员自己定义){函数的方法体代码}
 19             var nlist2 = list.FindAll(delegate (int p) { return p > 3; });
 20             foreach (var item in nlist2)
 21             {
 22                 Console.WriteLine(item);
 23             }
 24         }
  1        static bool Process(int p)
  2         {
  3             return p > 2;
  4         }
  5 
  6         static void W0401()
  7         {
  8             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  9             //直接传入一个方法名称也可以不过这个方法名称的参数以及返回值类型要跟
 10             //public delegate bool Predicate<in T>(T obj);一样
 11             var nlist =list.FindAll(Process);
 12             foreach (var item in nlist)
 13             {
 14                 Console.WriteLine(item);
 15             }
 16 
 17             Console.WriteLine("=================");
 18             //匿名方法的写法:delegate(编写匿名方法的参数,参数有程序员自己定义){函数的方法体代码}
 19             var nlist2 = list.FindAll(delegate (int p) { return p > 3; });
 20             foreach (var item in nlist2)
 21             {
 22                 Console.WriteLine(item);
 23             }
 24         }
 1        static bool Process(int p)
  2         {
  3             return p > 2;
  4         }
  5 
  6         static void W0401()
  7         {
  8             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  9             //直接传入一个方法名称也可以不过这个方法名称的参数以及返回值类型要跟
 10             //public delegate bool Predicate<in T>(T obj);一样
 11             var nlist =list.FindAll(Process);
 12             foreach (var item in nlist)
 13             {
 14                 Console.WriteLine(item);
 15             }
 16 
 17             Console.WriteLine("=================");
 18             //匿名方法的写法:delegate(编写匿名方法的参数,参数有程序员自己定义){函数的方法体代码}
 19             var nlist2 = list.FindAll(delegate (int p) { return p > 3; });
 20             foreach (var item in nlist2)
 21             {
 22                 Console.WriteLine(item);
 23             }
 24         }
  1  #region  Linq使用方法
  2         #region 1 Linq实现排序
  3         static void w0901()
  4         {
  5             var list = new List<Person>(){
  6                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
  7                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
  8                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
  9                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 10             };
 11             //需求:根据list集合中的age倒序排列后取出pig对象中的Name和Age
 12             //ascending :正序   descending:倒序
 13             var orderbyList = (from c in list
 14                                orderby c.Age descending
 15                                select new { c.Name, c.Age }).ToList();
 16             orderbyList.ForEach(c => Console.WriteLine(c.ToString()));
 17         }
 18         #endregion
 19 
 20         #region 2 LinqJoin实现集合的链接查询
 21         static void W0902()
 22         {
 23             var list = new List<Person>(){
 24                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 25                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 26                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 27                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 28             };
 29 
 30             var typelist = new List<PsersonType>()
 31             {
 32                 new PsersonType(){TypeID=1,TName="老师"},
 33                     new PsersonType(){TypeID=3,TName="学生"},
 34                     new PsersonType(){ TypeID=2, TName="保安"}
 35             };
 36 
 37             var nlist = (from c in list
 38                          join t in typelist
 39                          on c.TypeID equals t.TypeID
 40                          select new { c.Name, c.ID, t.TName }).ToString();
 41 
 42         }
 43         #endregion
 44 
 45         #region Linq分组
 46         static void W0903()
 47         {
 48             var list = new List<Person>(){
 49                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 50                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 51                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 52                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 53             };
 54 
 55             var groupbylist = (from c in list
 56                                group c by c.TypeID).ToList();
 57 
 58             groupbylist.ForEach(c =>
 59             {
 60                 Console.WriteLine("-----分组号=" + c.Key);
 61                 c.ToList().ForEach(g => Console.WriteLine(g.ToString()));
 62             });
 63 
 64         }
 65         #endregion
 66         #endregion

View Code

View Code

View Code

 

7、lambda表明式的测度以及书写方式

七、lambda表明式的预计以及书写方式

7、lambda表明式的臆度以及书写格局

 

亚洲必赢官网 37亚洲必赢官网 38

亚洲必赢官网 39亚洲必赢官网 40

亚洲必赢官网 41亚洲必赢官网 42

 

  1  List<Person> persons = new List<Person>() {
  2                 new Person { Name="张三",Age=12},
  3                  new Person { Name="李四",Age=32},
  4                   new Person { Name="王五",Age=44}
  5             };
  6             //使用匿名方法返回符合条件的数据。
  7             var nlistperson= persons.FindAll(delegate (Person person) { return person.Age > 22 && person.Name.Contains("李"); });
  8             //lambda表达式的推断使用goto语句来替换匿名函数   =>
  9             var nlistperson2 = persons.FindAll((Person person)=> { return person.Age > 22 && person.Name.Contains("李"); });
 10 
 11             /*
 12              1、lambda表达式的推断使用goto语句来替换匿名函数   => 继续简化
 13              2、当只有一个参数的时候可以省略()参数的括号,当返回值只有一句语句的时候可以省略return以及{}花括号
 14              3、当有多个参数的时候需要加上(),当有多条语句的时候需要添加{}每个语句后面加分号;最后返回的语句要加return
 15              */
 16             var nlistperson3 = persons.FindAll(person => person.Age > 22 && person.Name.Contains("李"));
 17 
 18             var nlistperson4 = persons.FindAll(person => {
 19                 person.Age++;
 20                 return person.Age > 22 && person.Name.Contains("李");
 21             });
 22 
 23             foreach (var item in nlistperson4)
 24             {
 25                 Console.WriteLine("名称:{0};年龄:{1}",item.Name,item.Age);
 26             }
  1  List<Person> persons = new List<Person>() {
  2                 new Person { Name="张三",Age=12},
  3                  new Person { Name="李四",Age=32},
  4                   new Person { Name="王五",Age=44}
  5             };
  6             //使用匿名方法返回符合条件的数据。
  7             var nlistperson= persons.FindAll(delegate (Person person) { return person.Age > 22 && person.Name.Contains("李"); });
  8             //lambda表达式的推断使用goto语句来替换匿名函数   =>
  9             var nlistperson2 = persons.FindAll((Person person)=> { return person.Age > 22 && person.Name.Contains("李"); });
 10 
 11             /*
 12              1、lambda表达式的推断使用goto语句来替换匿名函数   => 继续简化
 13              2、当只有一个参数的时候可以省略()参数的括号,当返回值只有一句语句的时候可以省略return以及{}花括号
 14              3、当有多个参数的时候需要加上(),当有多条语句的时候需要添加{}每个语句后面加分号;最后返回的语句要加return
 15              */
 16             var nlistperson3 = persons.FindAll(person => person.Age > 22 && person.Name.Contains("李"));
 17 
 18             var nlistperson4 = persons.FindAll(person => {
 19                 person.Age++;
 20                 return person.Age > 22 && person.Name.Contains("李");
 21             });
 22 
 23             foreach (var item in nlistperson4)
 24             {
 25                 Console.WriteLine("名称:{0};年龄:{1}",item.Name,item.Age);
 26             }
 1  List<Person> persons = new List<Person>() {
  2                 new Person { Name="张三",Age=12},
  3                  new Person { Name="李四",Age=32},
  4                   new Person { Name="王五",Age=44}
  5             };
  6             //使用匿名方法返回符合条件的数据。
  7             var nlistperson= persons.FindAll(delegate (Person person) { return person.Age > 22 && person.Name.Contains("李"); });
  8             //lambda表达式的推断使用goto语句来替换匿名函数   =>
  9             var nlistperson2 = persons.FindAll((Person person)=> { return person.Age > 22 && person.Name.Contains("李"); });
 10 
 11             /*
 12              1、lambda表达式的推断使用goto语句来替换匿名函数   => 继续简化
 13              2、当只有一个参数的时候可以省略()参数的括号,当返回值只有一句语句的时候可以省略return以及{}花括号
 14              3、当有多个参数的时候需要加上(),当有多条语句的时候需要添加{}每个语句后面加分号;最后返回的语句要加return
 15              */
 16             var nlistperson3 = persons.FindAll(person => person.Age > 22 && person.Name.Contains("李"));
 17 
 18             var nlistperson4 = persons.FindAll(person => {
 19                 person.Age++;
 20                 return person.Age > 22 && person.Name.Contains("李");
 21             });
 22 
 23             foreach (var item in nlistperson4)
 24             {
 25                 Console.WriteLine("名称:{0};年龄:{1}",item.Name,item.Age);
 26             }

 

View Code

View Code

View Code

 

捌、扩充方法

八、扩充方法

八、扩张方法

亚洲必赢官网 43亚洲必赢官网 44

亚洲必赢官网 45亚洲必赢官网 46

亚洲必赢官网 47亚洲必赢官网 48

  1  /// <summary>
  2         /// 1.扩展方法必须是一个静态方法
  3         /// 2.静态方法必须放在静态类中
  4         /// 3.扩展方法第一个参数必须是this开头,并且指定扩展方法是哪个类型上的
  5         /// 4.扩展方法只能有指定的类型才能点出来
  6         /// 5.扩展方法的this后面的参数不属于 方法的参数
  7         /// 6.如果扩展方法跟实例方法同名,先调用实例方法
  8         /// 7.子类可以调用父类的扩展方法
  9         /// 8.接口上的扩展方法可以被实现类的对象直接使用
 10         /// 9.扩展方法的本质,最终还是被编译器编译成了 静态类.静态方法()
 11         /// </summary>
 12         static void W0601()
 13         {
 14             DateTime dt = DateTime.Now;
 15             string fmtstr= dt.FmtDate();
 16         }
  1  /// <summary>
  2         /// 1.扩展方法必须是一个静态方法
  3         /// 2.静态方法必须放在静态类中
  4         /// 3.扩展方法第一个参数必须是this开头,并且指定扩展方法是哪个类型上的
  5         /// 4.扩展方法只能有指定的类型才能点出来
  6         /// 5.扩展方法的this后面的参数不属于 方法的参数
  7         /// 6.如果扩展方法跟实例方法同名,先调用实例方法
  8         /// 7.子类可以调用父类的扩展方法
  9         /// 8.接口上的扩展方法可以被实现类的对象直接使用
 10         /// 9.扩展方法的本质,最终还是被编译器编译成了 静态类.静态方法()
 11         /// </summary>
 12         static void W0601()
 13         {
 14             DateTime dt = DateTime.Now;
 15             string fmtstr= dt.FmtDate();
 16         }
1  /// <summary>
  2         /// 1.扩展方法必须是一个静态方法
  3         /// 2.静态方法必须放在静态类中
  4         /// 3.扩展方法第一个参数必须是this开头,并且指定扩展方法是哪个类型上的
  5         /// 4.扩展方法只能有指定的类型才能点出来
  6         /// 5.扩展方法的this后面的参数不属于 方法的参数
  7         /// 6.如果扩展方法跟实例方法同名,先调用实例方法
  8         /// 7.子类可以调用父类的扩展方法
  9         /// 8.接口上的扩展方法可以被实现类的对象直接使用
 10         /// 9.扩展方法的本质,最终还是被编译器编译成了 静态类.静态方法()
 11         /// </summary>
 12         static void W0601()
 13         {
 14             DateTime dt = DateTime.Now;
 15             string fmtstr= dt.FmtDate();
 16         }

View Code

View Code

View Code

亚洲必赢官网 49亚洲必赢官网 50

亚洲必赢官网 51亚洲必赢官网 52

亚洲必赢官网 53亚洲必赢官网 54

  1  public static class ExtHelper
  2     {
  3         public static string FmtDate(this DateTime dt)
  4         {
  5             return dt.ToString("YYYY-MM-DD HH:MM:ss");
  6         }
  7         public static string FmtDate(this DateTime dt,string str)
  8         {
  9             return dt.ToString("YYYY-MM-DD HH:MM:ss"+str);
 10         }
 11     }
  1  public static class ExtHelper
  2     {
  3         public static string FmtDate(this DateTime dt)
  4         {
  5             return dt.ToString("YYYY-MM-DD HH:MM:ss");
  6         }
  7         public static string FmtDate(this DateTime dt,string str)
  8         {
  9             return dt.ToString("YYYY-MM-DD HH:MM:ss"+str);
 10         }
 11     }
  1  public static class ExtHelper
  2     {
  3         public static string FmtDate(this DateTime dt)
  4         {
  5             return dt.ToString("YYYY-MM-DD HH:MM:ss");
  6         }
  7         public static string FmtDate(this DateTime dt,string str)
  8         {
  9             return dt.ToString("YYYY-MM-DD HH:MM:ss"+str);
 10         }
 11     }

View Code

View Code

View Code

9、系统内置的嘱托

九、系统内置的寄托

九、系统内置的嘱托

亚洲必赢官网 55亚洲必赢官网 56

亚洲必赢官网 57亚洲必赢官网 58

亚洲必赢官网 59亚洲必赢官网 60

  1  #region 系统内置委托
  2         #region Action委托 接收参数 无法回值
  3         static void W0701Action()
  4         {
  5             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
  6 
  7             list.ForEach(i => Console.WriteLine(i));
  8         }
  9         #endregion
 10 
 11         #region Predicate 泛型委托:接收一个参数,返回bool类型
 12         static void W0702Predicate()
 13         {
 14             var list = new List<Person>(){
 15                 new Person { Name = "张三", Age = 12 },
 16                 new Person { Name = "李四", Age = 32 },
 17                 new Person { Name = "王五", Age = 44 }
 18             };
 19 
 20             var nlist = list.FindAll(c => c.Age > 22);
 21             //获取当前list集合中,名字为张三的对象,如果有多个只返回第一个
 22 
 23             var person = list.Find(c => c.Name == "张三");
 24         }
 25         #endregion
 26 
 27         #region Func:接收参数,返回参数,但是不固定
 28         static void W0703Func()
 29         {
 30             var list = new List<Person>(){
 31                 new Person { Name = "张三", Age = 12 },
 32                 new Person { Name = "李四", Age = 32 },
 33                 new Person { Name = "王五", Age = 44 }
 34             };
 35 
 36             var nlist = list.Where(c => c.Age > 1);
 37             var nlist1 = nlist.ToList();
 38             nlist1.ForEach(c => Console.WriteLine(c.Name.ToString()));
 39         }
 40         #endregion
 41 
 42         #region System.Comparison<T> 返回一个整数,接收两个同类型的参数
 43         static void W0704Comparison()
 44         {
 45             var list = new List<Person>(){
 46                 new Person { Name = "张三", Age = 12 },
 47                 new Person { Name = "李四", Age = 32 },
 48                 new Person { Name = "王五", Age = 44 }
 49             };
 50 
 51             list.Sort((p, n) =>p.Age - n.Age);
 52             list.ForEach(c=>Console.WriteLine(c.Name.ToString()+":"+c.Age.ToString()));
 53         }
 54 
 55         #endregion
 56         #endregion
  1  #region 系统内置委托
  2         #region Action委托 接收参数 无法回值
  3         static void W0701Action()
  4         {
  5             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
  6 
  7             list.ForEach(i => Console.WriteLine(i));
  8         }
  9         #endregion
 10 
 11         #region Predicate 泛型委托:接收一个参数,返回bool类型
 12         static void W0702Predicate()
 13         {
 14             var list = new List<Person>(){
 15                 new Person { Name = "张三", Age = 12 },
 16                 new Person { Name = "李四", Age = 32 },
 17                 new Person { Name = "王五", Age = 44 }
 18             };
 19 
 20             var nlist = list.FindAll(c => c.Age > 22);
 21             //获取当前list集合中,名字为张三的对象,如果有多个只返回第一个
 22 
 23             var person = list.Find(c => c.Name == "张三");
 24         }
 25         #endregion
 26 
 27         #region Func:接收参数,返回参数,但是不固定
 28         static void W0703Func()
 29         {
 30             var list = new List<Person>(){
 31                 new Person { Name = "张三", Age = 12 },
 32                 new Person { Name = "李四", Age = 32 },
 33                 new Person { Name = "王五", Age = 44 }
 34             };
 35 
 36             var nlist = list.Where(c => c.Age > 1);
 37             var nlist1 = nlist.ToList();
 38             nlist1.ForEach(c => Console.WriteLine(c.Name.ToString()));
 39         }
 40         #endregion
 41 
 42         #region System.Comparison<T> 返回一个整数,接收两个同类型的参数
 43         static void W0704Comparison()
 44         {
 45             var list = new List<Person>(){
 46                 new Person { Name = "张三", Age = 12 },
 47                 new Person { Name = "李四", Age = 32 },
 48                 new Person { Name = "王五", Age = 44 }
 49             };
 50 
 51             list.Sort((p, n) =>p.Age - n.Age);
 52             list.ForEach(c=>Console.WriteLine(c.Name.ToString()+":"+c.Age.ToString()));
 53         }
 54 
 55         #endregion
 56         #endregion
1  #region 系统内置委托
  2         #region Action委托 接收参数 无法回值
  3         static void W0701Action()
  4         {
  5             List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
  6 
  7             list.ForEach(i => Console.WriteLine(i));
  8         }
  9         #endregion
 10 
 11         #region Predicate 泛型委托:接收一个参数,返回bool类型
 12         static void W0702Predicate()
 13         {
 14             var list = new List<Person>(){
 15                 new Person { Name = "张三", Age = 12 },
 16                 new Person { Name = "李四", Age = 32 },
 17                 new Person { Name = "王五", Age = 44 }
 18             };
 19 
 20             var nlist = list.FindAll(c => c.Age > 22);
 21             //获取当前list集合中,名字为张三的对象,如果有多个只返回第一个
 22 
 23             var person = list.Find(c => c.Name == "张三");
 24         }
 25         #endregion
 26 
 27         #region Func:接收参数,返回参数,但是不固定
 28         static void W0703Func()
 29         {
 30             var list = new List<Person>(){
 31                 new Person { Name = "张三", Age = 12 },
 32                 new Person { Name = "李四", Age = 32 },
 33                 new Person { Name = "王五", Age = 44 }
 34             };
 35 
 36             var nlist = list.Where(c => c.Age > 1);
 37             var nlist1 = nlist.ToList();
 38             nlist1.ForEach(c => Console.WriteLine(c.Name.ToString()));
 39         }
 40         #endregion
 41 
 42         #region System.Comparison<T> 返回一个整数,接收两个同类型的参数
 43         static void W0704Comparison()
 44         {
 45             var list = new List<Person>(){
 46                 new Person { Name = "张三", Age = 12 },
 47                 new Person { Name = "李四", Age = 32 },
 48                 new Person { Name = "王五", Age = 44 }
 49             };
 50 
 51             list.Sort((p, n) =>p.Age - n.Age);
 52             list.ForEach(c=>Console.WriteLine(c.Name.ToString()+":"+c.Age.ToString()));
 53         }
 54 
 55         #endregion
 56         #endregion

View Code

View Code

View Code

10、SQO方法 -典型查询运算符

拾、SQO方法 -标准查询运算符

拾、SQO方法 -规范查询运算符

始建多个类

开创七个类

创办七个类

亚洲必赢官网 61亚洲必赢官网 62

亚洲必赢官网 63亚洲必赢官网 64

亚洲必赢官网 65亚洲必赢官网 66

  1  public class Person
  2     {
  3         public int TypeID { get; set; }
  4 
  5         public int ID { get; set; }
  6         //自动属性
  7         public string Name { get; set; }
  8 
  9         private int _age;
 10 
 11         public int Age {
 12             get { return _age; }
 13             set { _age = value; }
 14         }
 15 
 16         public override string ToString()
 17         {
 18             return "ID=" + this.ID + "名称=" + this.Name + ",年龄=" + this.Age + "所属类别=" + this.TypeID;
 19         }
 20     }
 21 
 22     public class PsersonType
 23     {
 24         public int TypeID { get; set; }
 25         public string TName { get; set; }
 26     }
  1  public class Person
  2     {
  3         public int TypeID { get; set; }
  4 
  5         public int ID { get; set; }
  6         //自动属性
  7         public string Name { get; set; }
  8 
  9         private int _age;
 10 
 11         public int Age {
 12             get { return _age; }
 13             set { _age = value; }
 14         }
 15 
 16         public override string ToString()
 17         {
 18             return "ID=" + this.ID + "名称=" + this.Name + ",年龄=" + this.Age + "所属类别=" + this.TypeID;
 19         }
 20     }
 21 
 22     public class PsersonType
 23     {
 24         public int TypeID { get; set; }
 25         public string TName { get; set; }
 26     }
  1  public class Person
  2     {
  3         public int TypeID { get; set; }
  4 
  5         public int ID { get; set; }
  6         //自动属性
  7         public string Name { get; set; }
  8 
  9         private int _age;
 10 
 11         public int Age {
 12             get { return _age; }
 13             set { _age = value; }
 14         }
 15 
 16         public override string ToString()
 17         {
 18             return "ID=" + this.ID + "名称=" + this.Name + ",年龄=" + this.Age + "所属类别=" + this.TypeID;
 19         }
 20     }
 21 
 22     public class PsersonType
 23     {
 24         public int TypeID { get; set; }
 25         public string TName { get; set; }
 26     }

View Code

View Code

View Code

SQO常用方法的应用 查询、排序、关联、分组等

SQO常用方法的施用 查询、排序、关联、分组等

SQO常用方法的使用 查询、排序、关联、分组等

亚洲必赢官网 67亚洲必赢官网 68

亚洲必赢官网 69亚洲必赢官网 70

亚洲必赢官网 71亚洲必赢官网 72

  1         #region SQO方法 -标准查询运算符
  2         #region 1 where() firstordefult() LastOrDefault方法进行查找操作 (链式编程)
  3         static void W0801()
  4         {
  5             var list = new List<Person>(){
  6                 new Person { Name = "张三", Age = 12 },
  7                 new Person { Name = "李四", Age = 32 },
  8                 new Person { Name = "王五", Age = 44 }
  9             };
 10 
 11             //利用where查找出list中年龄大于22或者名称叫王五
 12             //链式编程
 13             list.Where(c => c.Age > 5 || c.Name == "王五").ToList().ForEach(c => Console.WriteLine(c.ToString()));
 14 
 15             //firstordefult()的演示
 16            var person= list.FirstOrDefault(c => c.Age > 5 || c.Name == "王五");
 17             Console.WriteLine(person.ToString());
 18 
 19             //获取集合中满足条件的最后一个元素
 20             var person2 = list.LastOrDefault(c => c.Age > 5 || c.Name == "王五");
 21             Console.WriteLine(person.ToString());
 22 
 23         }
 24         #endregion
 25 
 26         #region 2 对集合进行排序(正序和倒序)
 27         static void W0802()
 28         {
 29             var list = new List<Person>(){
 30                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 31                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 32                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 33             };
 34             //正序
 35             list.OrderBy(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 36             Console.WriteLine("---------");
 37             //倒序
 38             list.OrderByDescending(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 39         }
 40         #endregion
 41 
 42         #region 3 根据多个字段的组合排序
 43         static void W0803()
 44         {
 45             var list = new List<Person>(){
 46                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 47                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 48                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 49             };
 50             //多个条件进行正序 先按照年龄然后按照ID正序
 51             list.OrderBy(c => c.Age).ThenBy(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 52 
 53             //多条件倒序
 54             list.OrderByDescending(c => c.Age).ThenByDescending(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 55         }
 56         #endregion
 57 
 58         #region 4 投影方法(Select) ( 重点 )
 59         static void W0804()
 60         {
 61             var list = new List<Person>(){
 62                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 63                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 64                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 65             };
 66             //取出list集合里面的部分字段
 67             List<string> nlist = list.Select(c => c.Name).ToList();
 68 
 69             //重新创建一个新的list集合只包含name和age
 70             list.Select(c => new { N = c.Name, A = c.Age }).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 71         }
 72         #endregion
 73 
 74         #region 5 分页方法 Skip(skipNum).Take(页容量)  Skip():表示跳过集合中的前面多少行
 75         static void W0805()
 76         {
 77             int rowcount = 0;
 78             W0805ByPage(1, 2, out rowcount).ForEach(c => Console.WriteLine(c.ToString()));
 79         }
 80         static List<Person> W0805ByPage(int pageindex, int pagesize, out int rowcount)
 81         {
 82             var list = new List<Person>(){
 83                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 84                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 85                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 86             };
 87            // 给rowcount赋予满足条件的数据总行数
 88             rowcount = list.Count();
 89             //经过第几页跳过跳过多少条
 90             int skipCount = (pageindex - 1) * pagesize;
 91             return list.Where(c => c.Age > 1).Skip(skipCount).Take(pagesize).ToList();
 92         }
 93         #endregion
 94 
 95         #region 6 连表查询Join
 96         static void W0806()
 97         {
 98             var list = new List<Person>(){
 99                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
100                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
101                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
102                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
103             };
104 
105             var typelist = new List<PsersonType>()
106             {
107                 new PsersonType(){TypeID=1,TName="老师"},
108                     new PsersonType(){TypeID=3,TName="学生"},
109                     new PsersonType(){ TypeID=2, TName="保安"}
110             };
111 
112             //利用SQO方法Join() 来实现上述业务逻辑
113             var nlist = list.Join(typelist, p => p.TypeID, c => c.TypeID, (p, c) =>new { p.Name,p.Age,c.TName }).ToList();
114 
115             nlist.ForEach(c => Console.WriteLine(c.ToString()));
116         }
117         #endregion
118 
119         #region 7 分组 GroupBy()
120         static void W0807()
121         {
122             var list = new List<Person>(){
123                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
124                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
125                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
126                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
127             };
128 
129             //利用SQO方法进行分组操作
130             list.GroupBy(c => c.TypeID).ToList().ForEach(c => {
131                 Console.WriteLine("----分组号{0}" + c.Key,c.ToList().Count());
132                 c.ToList().ForEach(g => Console.WriteLine(g.ToString()));
133             });
134         }
135         #endregion
136         #endregion
  1         #region SQO方法 -标准查询运算符
  2         #region 1 where() firstordefult() LastOrDefault方法进行查找操作 (链式编程)
  3         static void W0801()
  4         {
  5             var list = new List<Person>(){
  6                 new Person { Name = "张三", Age = 12 },
  7                 new Person { Name = "李四", Age = 32 },
  8                 new Person { Name = "王五", Age = 44 }
  9             };
 10 
 11             //利用where查找出list中年龄大于22或者名称叫王五
 12             //链式编程
 13             list.Where(c => c.Age > 5 || c.Name == "王五").ToList().ForEach(c => Console.WriteLine(c.ToString()));
 14 
 15             //firstordefult()的演示
 16            var person= list.FirstOrDefault(c => c.Age > 5 || c.Name == "王五");
 17             Console.WriteLine(person.ToString());
 18 
 19             //获取集合中满足条件的最后一个元素
 20             var person2 = list.LastOrDefault(c => c.Age > 5 || c.Name == "王五");
 21             Console.WriteLine(person.ToString());
 22 
 23         }
 24         #endregion
 25 
 26         #region 2 对集合进行排序(正序和倒序)
 27         static void W0802()
 28         {
 29             var list = new List<Person>(){
 30                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 31                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 32                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 33             };
 34             //正序
 35             list.OrderBy(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 36             Console.WriteLine("---------");
 37             //倒序
 38             list.OrderByDescending(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 39         }
 40         #endregion
 41 
 42         #region 3 根据多个字段的组合排序
 43         static void W0803()
 44         {
 45             var list = new List<Person>(){
 46                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 47                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 48                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 49             };
 50             //多个条件进行正序 先按照年龄然后按照ID正序
 51             list.OrderBy(c => c.Age).ThenBy(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 52 
 53             //多条件倒序
 54             list.OrderByDescending(c => c.Age).ThenByDescending(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 55         }
 56         #endregion
 57 
 58         #region 4 投影方法(Select) ( 重点 )
 59         static void W0804()
 60         {
 61             var list = new List<Person>(){
 62                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 63                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 64                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 65             };
 66             //取出list集合里面的部分字段
 67             List<string> nlist = list.Select(c => c.Name).ToList();
 68 
 69             //重新创建一个新的list集合只包含name和age
 70             list.Select(c => new { N = c.Name, A = c.Age }).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 71         }
 72         #endregion
 73 
 74         #region 5 分页方法 Skip(skipNum).Take(页容量)  Skip():表示跳过集合中的前面多少行
 75         static void W0805()
 76         {
 77             int rowcount = 0;
 78             W0805ByPage(1, 2, out rowcount).ForEach(c => Console.WriteLine(c.ToString()));
 79         }
 80         static List<Person> W0805ByPage(int pageindex, int pagesize, out int rowcount)
 81         {
 82             var list = new List<Person>(){
 83                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 84                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 85                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 86             };
 87            // 给rowcount赋予满足条件的数据总行数
 88             rowcount = list.Count();
 89             //经过第几页跳过跳过多少条
 90             int skipCount = (pageindex - 1) * pagesize;
 91             return list.Where(c => c.Age > 1).Skip(skipCount).Take(pagesize).ToList();
 92         }
 93         #endregion
 94 
 95         #region 6 连表查询Join
 96         static void W0806()
 97         {
 98             var list = new List<Person>(){
 99                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
100                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
101                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
102                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
103             };
104 
105             var typelist = new List<PsersonType>()
106             {
107                 new PsersonType(){TypeID=1,TName="老师"},
108                     new PsersonType(){TypeID=3,TName="学生"},
109                     new PsersonType(){ TypeID=2, TName="保安"}
110             };
111 
112             //利用SQO方法Join() 来实现上述业务逻辑
113             var nlist = list.Join(typelist, p => p.TypeID, c => c.TypeID, (p, c) =>new { p.Name,p.Age,c.TName }).ToList();
114 
115             nlist.ForEach(c => Console.WriteLine(c.ToString()));
116         }
117         #endregion
118 
119         #region 7 分组 GroupBy()
120         static void W0807()
121         {
122             var list = new List<Person>(){
123                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
124                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
125                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
126                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
127             };
128 
129             //利用SQO方法进行分组操作
130             list.GroupBy(c => c.TypeID).ToList().ForEach(c => {
131                 Console.WriteLine("----分组号{0}" + c.Key,c.ToList().Count());
132                 c.ToList().ForEach(g => Console.WriteLine(g.ToString()));
133             });
134         }
135         #endregion
136         #endregion
 1         #region SQO方法 -标准查询运算符
  2         #region 1 where() firstordefult() LastOrDefault方法进行查找操作 (链式编程)
  3         static void W0801()
  4         {
  5             var list = new List<Person>(){
  6                 new Person { Name = "张三", Age = 12 },
  7                 new Person { Name = "李四", Age = 32 },
  8                 new Person { Name = "王五", Age = 44 }
  9             };
 10 
 11             //利用where查找出list中年龄大于22或者名称叫王五
 12             //链式编程
 13             list.Where(c => c.Age > 5 || c.Name == "王五").ToList().ForEach(c => Console.WriteLine(c.ToString()));
 14 
 15             //firstordefult()的演示
 16            var person= list.FirstOrDefault(c => c.Age > 5 || c.Name == "王五");
 17             Console.WriteLine(person.ToString());
 18 
 19             //获取集合中满足条件的最后一个元素
 20             var person2 = list.LastOrDefault(c => c.Age > 5 || c.Name == "王五");
 21             Console.WriteLine(person.ToString());
 22 
 23         }
 24         #endregion
 25 
 26         #region 2 对集合进行排序(正序和倒序)
 27         static void W0802()
 28         {
 29             var list = new List<Person>(){
 30                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 31                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 32                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 33             };
 34             //正序
 35             list.OrderBy(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 36             Console.WriteLine("---------");
 37             //倒序
 38             list.OrderByDescending(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 39         }
 40         #endregion
 41 
 42         #region 3 根据多个字段的组合排序
 43         static void W0803()
 44         {
 45             var list = new List<Person>(){
 46                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 47                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 48                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 49             };
 50             //多个条件进行正序 先按照年龄然后按照ID正序
 51             list.OrderBy(c => c.Age).ThenBy(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 52 
 53             //多条件倒序
 54             list.OrderByDescending(c => c.Age).ThenByDescending(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 55         }
 56         #endregion
 57 
 58         #region 4 投影方法(Select) ( 重点 )
 59         static void W0804()
 60         {
 61             var list = new List<Person>(){
 62                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 63                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 64                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 65             };
 66             //取出list集合里面的部分字段
 67             List<string> nlist = list.Select(c => c.Name).ToList();
 68 
 69             //重新创建一个新的list集合只包含name和age
 70             list.Select(c => new { N = c.Name, A = c.Age }).ToList().ForEach(c => Console.WriteLine(c.ToString()));
 71         }
 72         #endregion
 73 
 74         #region 5 分页方法 Skip(skipNum).Take(页容量)  Skip():表示跳过集合中的前面多少行
 75         static void W0805()
 76         {
 77             int rowcount = 0;
 78             W0805ByPage(1, 2, out rowcount).ForEach(c => Console.WriteLine(c.ToString()));
 79         }
 80         static List<Person> W0805ByPage(int pageindex, int pagesize, out int rowcount)
 81         {
 82             var list = new List<Person>(){
 83                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 84                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 85                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}
 86             };
 87            // 给rowcount赋予满足条件的数据总行数
 88             rowcount = list.Count();
 89             //经过第几页跳过跳过多少条
 90             int skipCount = (pageindex - 1) * pagesize;
 91             return list.Where(c => c.Age > 1).Skip(skipCount).Take(pagesize).ToList();
 92         }
 93         #endregion
 94 
 95         #region 6 连表查询Join
 96         static void W0806()
 97         {
 98             var list = new List<Person>(){
 99                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
100                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
101                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
102                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
103             };
104 
105             var typelist = new List<PsersonType>()
106             {
107                 new PsersonType(){TypeID=1,TName="老师"},
108                     new PsersonType(){TypeID=3,TName="学生"},
109                   

View Code

View Code

View Code

十1、linq的常用方法

101、linq的常用方法

10一、linq的常用方法

亚洲必赢官网 73亚洲必赢官网 74

亚洲必赢官网 75亚洲必赢官网 76

亚洲必赢官网 77亚洲必赢官网 78

  1  #region  Linq使用方法
  2         #region 1 Linq实现排序
  3         static void w0901()
  4         {
  5             var list = new List<Person>(){
  6                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
  7                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
  8                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
  9                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 10             };
 11             //需求:根据list集合中的age倒序排列后取出pig对象中的Name和Age
 12             //ascending :正序   descending:倒序
 13             var orderbyList = (from c in list
 14                                orderby c.Age descending
 15                                select new { c.Name, c.Age }).ToList();
 16             orderbyList.ForEach(c => Console.WriteLine(c.ToString()));
 17         }
 18         #endregion
 19 
 20         #region 2 LinqJoin实现集合的链接查询
 21         static void W0902()
 22         {
 23             var list = new List<Person>(){
 24                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 25                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 26                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 27                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 28             };
 29 
 30             var typelist = new List<PsersonType>()
 31             {
 32                 new PsersonType(){TypeID=1,TName="老师"},
 33                     new PsersonType(){TypeID=3,TName="学生"},
 34                     new PsersonType(){ TypeID=2, TName="保安"}
 35             };
 36 
 37             var nlist = (from c in list
 38                          join t in typelist
 39                          on c.TypeID equals t.TypeID
 40                          select new { c.Name, c.ID, t.TName }).ToString();
 41 
 42         }
 43         #endregion
 44 
 45         #region Linq分组
 46         static void W0903()
 47         {
 48             var list = new List<Person>(){
 49                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 50                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 51                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 52                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 53             };
 54 
 55             var groupbylist = (from c in list
 56                                group c by c.TypeID).ToList();
 57 
 58             groupbylist.ForEach(c =>
 59             {
 60                 Console.WriteLine("-----分组号=" + c.Key);
 61                 c.ToList().ForEach(g => Console.WriteLine(g.ToString()));
 62             });
 63 
 64         }
 65         #endregion
 66         #endregion
  1  #region  Linq使用方法
  2         #region 1 Linq实现排序
  3         static void w0901()
  4         {
  5             var list = new List<Person>(){
  6                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
  7                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
  8                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
  9                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 10             };
 11             //需求:根据list集合中的age倒序排列后取出pig对象中的Name和Age
 12             //ascending :正序   descending:倒序
 13             var orderbyList = (from c in list
 14                                orderby c.Age descending
 15                                select new { c.Name, c.Age }).ToList();
 16             orderbyList.ForEach(c => Console.WriteLine(c.ToString()));
 17         }
 18         #endregion
 19 
 20         #region 2 LinqJoin实现集合的链接查询
 21         static void W0902()
 22         {
 23             var list = new List<Person>(){
 24                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 25                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 26                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 27                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 28             };
 29 
 30             var typelist = new List<PsersonType>()
 31             {
 32                 new PsersonType(){TypeID=1,TName="老师"},
 33                     new PsersonType(){TypeID=3,TName="学生"},
 34                     new PsersonType(){ TypeID=2, TName="保安"}
 35             };
 36 
 37             var nlist = (from c in list
 38                          join t in typelist
 39                          on c.TypeID equals t.TypeID
 40                          select new { c.Name, c.ID, t.TName }).ToString();
 41 
 42         }
 43         #endregion
 44 
 45         #region Linq分组
 46         static void W0903()
 47         {
 48             var list = new List<Person>(){
 49                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 50                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 51                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 52                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 53             };
 54 
 55             var groupbylist = (from c in list
 56                                group c by c.TypeID).ToList();
 57 
 58             groupbylist.ForEach(c =>
 59             {
 60                 Console.WriteLine("-----分组号=" + c.Key);
 61                 c.ToList().ForEach(g => Console.WriteLine(g.ToString()));
 62             });
 63 
 64         }
 65         #endregion
 66         #endregion
  1  #region  Linq使用方法
  2         #region 1 Linq实现排序
  3         static void w0901()
  4         {
  5             var list = new List<Person>(){
  6                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
  7                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
  8                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
  9                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 10             };
 11             //需求:根据list集合中的age倒序排列后取出pig对象中的Name和Age
 12             //ascending :正序   descending:倒序
 13             var orderbyList = (from c in list
 14                                orderby c.Age descending
 15                                select new { c.Name, c.Age }).ToList();
 16             orderbyList.ForEach(c => Console.WriteLine(c.ToString()));
 17         }
 18         #endregion
 19 
 20         #region 2 LinqJoin实现集合的链接查询
 21         static void W0902()
 22         {
 23             var list = new List<Person>(){
 24                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 25                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 26                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 27                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 28             };
 29 
 30             var typelist = new List<PsersonType>()
 31             {
 32                 new PsersonType(){TypeID=1,TName="老师"},
 33                     new PsersonType(){TypeID=3,TName="学生"},
 34                     new PsersonType(){ TypeID=2, TName="保安"}
 35             };
 36 
 37             var nlist = (from c in list
 38                          join t in typelist
 39                          on c.TypeID equals t.TypeID
 40                          select new { c.Name, c.ID, t.TName }).ToString();
 41 
 42         }
 43         #endregion
 44 
 45         #region Linq分组
 46         static void W0903()
 47         {
 48             var list = new List<Person>(){
 49                 new Person {ID=1, Name = "张三", Age = 12,TypeID=1 },
 50                 new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2},
 51                 new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1},
 52                  new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3}
 53             };
 54 
 55             var groupbylist = (from c in list
 56                                group c by c.TypeID).ToList();
 57 
 58             groupbylist.ForEach(c =>
 59             {
 60                 Console.WriteLine("-----分组号=" + c.Key);
 61                 c.ToList().ForEach(g => Console.WriteLine(g.ToString()));
 62             });
 63 
 64         }
 65         #endregion
 66         #endregion

View Code

View Code

View Code

网站地图xml地图