Lambda
一.Lambda的演变
Lambda的演变,从下面的类中可以看出,.Net Framwork1.0时还是用方法实例化委托的,2.0的时候出现了匿名方法,3.0的时候出现了Lambda。
lambda组成是:左边(参数列表)+中间( ()=>符号,表示gose to)+右边(方法体)。无论怎么演变,lambda本质上还是一个方法。
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace _15_linq
8 {
9 ///
二.从IL层面解读委托
lambda:
实际上是一个类中类,里面的一个internal方法,然后被绑定到静态的委托类型字段
三.Linq
.Net Framework3.0出现了匿名方法,匿名类,lambda,var,扩展方法,这些都是为linq服务的。
1.扩展方法
扩展方法:静态类里面的静态方法,第一个参数类型前面加上this。
*扩展方法用途:可以不修改类,或者没办法修改类的情况下,给类添加方法。*
1 using System;
2 using System.Runtime.CompilerServices;
3 using System.Threading;
4 using System.Threading.Tasks;
5
6 namespace ConsoleApp1
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Student student = new Student
13 {
14 Name = "王小二",
15 Id = 1,
16 Age = 33,
17 };
18 student.Study();
19 student.Sing
20 bool result = 123.Then(12);//扩展方法
21 Console.ReadKey();
22 }
23 }
24 public static class Extend
25 {
26 public static void Sing
27 {
28 Console.WriteLine($"{source.Name}:Sing a Song");
29 }
30 public static bool Then(this int int1, int int2)
31 {
32 return int1 > int2;
33 }
34 }
35 ///
2.匿名类
匿名类,在匿名类的语法中,并没有为其命名,而是直接一个new{ }了事的。
var ,object,dynamic这三个是啥?
1>var 是由编译器自动推算的
2>object是一个具体的类型
3>dynamic 主要就是避开编译器的检查
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 //匿名类
6 /*var:
7 * 1>是一个语法糖,由编译器自动推算
8 * 2>var必须在声明的时候就确定类型,类型确定后就不能修改
9 * 3>配合匿名类类型一起使用*/
10 var student = new
11 {
12 Id=1,
13 Name="匿名类",
14 Age=35,
15 };
16 Console.WriteLine($"匿名类:{student.Id},{student.Name},{student.Age}");
17
18 //object是一种具体类型,不存在Id或者其他属性的
19 object ostudent = new
20 {
21 Id = 2,
22 Name = "object类型",
23 Age = 36,
24 };
25 //Console.WriteLine(ostudent.Id); //object是一种类型,不存在Id或者其他属性的
26
27 //dynamic就是避开编译器的检查
28 dynamic dStudent = new
29 {
30 Id = 1,
31 Name = "dynamic类型",
32 Age = 35,
33 };
34 Console.WriteLine($"dynamic类型:{dStudent.Id},{dStudent.Name},{dStudent.Age}");
35 Console.ReadKey();
36 }
37 }
下面开始进入正题,说说linq,以及一些常用的:
1>过滤小能手:Where方法
Where完成对数据集合的过滤,需要提供一个带bool返回值的“筛选器”(匿名方法,委托,lambda表达式都可以),从而表明数据集合中某个元素是否被返回。
2>投影小行家Select方法
Select是完成对数据的转换,返回新的对象集合。
3>排序小牛OrderBy
OrderBy是完成对数据的排序
4>连接小助手Join
Join连接两个类之间的关联联系
5>分组教授GroupBy
GroupBy对数据集合进行分类
下面代码对于上面进行了验证,运行结果如下
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 namespace ConsoleApp1
5 {
6 class Program
7 {
8 public delegate bool MyDelegate(Student student);
9 static void Main(string[] args)
10 {
11 LinqShow linkShow = new LinqShow();
12 linkShow.Show();
13 Console.ReadKey();
14 }
15 }
16 public class LinqShow
17 {
18 #region 先准备一堆学生
19 static List
20 {
21 List
22 {
23 new Student(){Id=4,Gender= true, Name="小四", Age=33,},
24 new Student(){Id=1,Gender= false, Name="王小一", Age=30,},
25 new Student(){Id=2,Gender= true, Name="王小二", Age=31,},
26 new Student(){Id=3,Gender= false, Name="王小三", Age=32,},
27 };
28 return listStudent;
29 }
30 static List
31 {
32 List
33 {
34 new Class()
35 {
36 ClassId=1,
37 ClassName="初级班",
38 },
39 new Class()
40 {
41 ClassId=2,
42 ClassName="高级班",
43 },
44 new Class()
45 {
46 ClassId=3,
47 ClassName="架构班",
48 },
49 new Class()
50 {
51 ClassId=4,
52 ClassName="微信班",
53 },
54 };
55 return listClass;
56 }
57 #endregion
58
59 ///
手机扫一扫
移动阅读更方便
你可能感兴趣的文章