【美高梅开户网址】知识点总计,MVC学习以前务必精晓的c

壹.类电动属性

1.类活动属性

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的定义方法是同样的,只可是是微软电动帮您生成了字段。

Name为类的全自动属性,其实跟下边age的定义方法是平等的,只可是是微软活动帮你生成了字段。

Name为类的自行属性,其实跟上边age的定义方法是同样的,只不过是微软机动帮你生成了字段。

美高梅开户网址 ,3.无法将null赋值给var类型。

贰.隐式类型

2.隐式类型

二.隐式类型

4.语句中只声Bellamy次变量,证明后无法退换项目。

美高梅开户网址 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         }

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

View Code

View Code

View Code

二.当无名类的参数个数同样的时候是集体1个泛型类。

1.var类型会被编写翻译器依照开始值的品类自动测算出切实可行的类型。

壹.var类型会被编写翻译器依据初始值的品种自动测算出切实可行的花色。

1.var类型会被编写翻译器依照开端值的类别自动测算出切实可行的品类。

三.当无名氏类的参数个数不1致的时候是独家生成区别的泛型类。

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

四.无名类起头化的时候自然要赋值。

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

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

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

 

四.语句中只声多美滋(Dumex)(Beingmate)次变量,表明后不可能改换项目。

四.语句中只声美素佳儿(Friso)次变量,注脚后不能够更动项目。

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

四.暗许值和命名参数

三、匿名类

三、匿名类

三、匿名类

 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.当贰个办法里面包车型大巴参数有默许值的时候,能够直接调用方法毫无传值,编写翻译器会自动调用参数的值。

  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         }

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

View Code

View Code

View Code

三.假若须要给某1个参数赋值,书写的格式是 参数名:值。

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

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

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

【美高梅开户网址】知识点总计,MVC学习以前务必精晓的c。 

贰.当无名类的参数个数同样的时候是公共一个泛型类。

二.当无名类的参数个数同样的时候是公私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         }

肆.无名氏类初步化的时候一定要赋值。

肆.无名氏类伊始化的时候势需求赋值。

4.佚名类发轫化的时候一定要赋值。

1.对象/集合开首化器,能够在new3个目的也许聚众的时候,直接在末端加{}里面对品质直接赋值或然直接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         }

壹.当一个格局里面包车型客车参数有暗中认可值的时候,能够一贯调用方法毫无传值,编写翻译器会自动调用参数的值。

壹.当四个艺术里面包车型大巴参数有私下认可值的时候,可以一贯调用方法毫无传值,编写翻译器会自动调用参数的值。

一.当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.当方法有暗中同意值时,假使调用的时候只传入第1个参数,那么编写翻译器会把第贰个参数设置为私下认可值。

二.当方法有暗许值时,要是调用的时候只传入第一个参数,那么编写翻译器会把第1个参数设置为暗许值。

2.当形式有暗许值时,假若调用的时候只传入第3个参数,那么编写翻译器会把首个参数设置为私下认可值。

 

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

叁.借使急需给某二个参数赋值,书写的格式是 参数名:值。

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

7系统内置的寄托

伍、对象/集结起头化器

伍、对象/集结伊始化器

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个目的大概聚众的时候,直接在背后加{}里面对品质直接赋值也许直接new二个目的给集结。

一.目标/集结初叶化器,能够在new1个对象恐怕聚众的时候,直接在前边加{}里面对质量间接赋值恐怕直接new二个对象给集结。

一.对象/集结起先化器,能够在new多少个目的恐怕聚众的时候,直接在后头加{}里面对质量直接赋值或然直接new一个目的给集合。

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

6、匿名形式(无名函数、佚名委托)

陆、佚名形式(佚名函数、无名氏委托)

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表达式的测算以及书写格局

7、lambda表达式的预计以及书写格局

柒、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

九、系统内置的委托

九、系统内置的信托

九、系统内置的委托

美高梅开户网址 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方法 -标准查询运算符

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

10、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

十一、linq的常用方法

拾一、linq的常用方法

拾1、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地图