Linq 表达


Lambda 简单了解

      //Lambda 

            //匿名方法
            //delegate (Student s) { return s.Age > 12 && s.Age < 20; };
            //Lable 表达式 (代替了上面的)
            //s => s.Age > 12 && s.Age < 20

            //在Lambda Expression 中指定多个参数
            //(s, youngAge) => s.Age >= youngage;
            //指定参数类型
            //(Student s, int youngAge) => s.Age >= youngage;

            //没有参数的Lambda表达式
            //() => Console.WriteLine("Parameter less lambda expression")

            //多语句Lambda表达式
            //(s, youngAge) =>
            //{
            //    Console.WriteLine("Lambda expression with multiple statements in the body");
            //    Return s.Age >= youngAge;
            //}

将Lambda表达式分配给委派

lambda表达式可以分配给Func类型委托。Func委托中的最后一个参数类型是返回类型,其余是输入参数。而Action 类型委托,只有输入参数,不返回。

例如:

  Funcbool> isStudentTeenAger = x => x.Age > 12 && x.Age < 20;
            Student std = new Student { Age = 21 };
            bool isTeen = isStudentTeenAger(std);//false

这个委托我们可以来用方法表示

 bool isStudentTeenAger(Student s)
        {
            return s.Age > 12 && s.Age < 20;
        }

Action:

  Action PrintStudentDetail = s => Console.WriteLine("Name: {0}, Age: {1} ", s.StudentName, s.Age);
            Student std = new Student() { StudentName = "Bill", Age = 21 };
            PrintStudentDetail(std);//output: Name: Bill, Age: 21

Linq中的Func:

  IList studentList = new List() {
                new Student { Age=15, StudentName="sealee"  },
                new Student { Age=21, StudentName="sealee2"  },
            };

            Funcbool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;
            var teenStudents = studentList.Where(isStudentTeenAger).ToList();

LINQ中的表达式

我们已经了解到lambda Expression可以分配给Func或Action类型委托来处理内存中的集合。.NET编译器在编译时将分配给Func或Action类型委托的lambda表达式转换为可执行代码。

LINQ引入了名为Expression的新类型,它表示强类型的lambda表达式。这意味着lambda表达式也可以分配给Expression 类型。.NET编译器将分配给Expression 的lambda表达式转换为表达式树而不是可执行代码。

  //定义表达式
            //Func 方法
           // Func isTeenAger = s => s.Age > 12 && s.Age < 20;
            //使用Expresson包装Func委托将上述Func类型委托转换为表达式,如下所示:
            Expressionbool>> isTeenAgerExpr = s => s.Age > 12 && s.Age < 20;

            //调用表达式
            Funcbool> isTeenAger = isTeenAgerExpr.Compile();
            //Invoke
            bool result = isTeenAger(new Student() { ID = 1, StudentName = "Steve", Age = 20 });

使用:

  //IEnumerable  studentList
            IList studentList = new List() {
                new Student { Age=15, StudentName="sealee"  },
                new Student { Age=21, StudentName="sealee2"  },
            };
            Funcbool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;
            //OutDto  是我们自己定义的返回类    实际情况一般针对我们的返回接口都会生成一个单独的Dto来返回
            Expression> outDto = 
                s => new OutDto { AAgeDto = s.Age, StudentNameDto = s.StudentName };
            var teenStudents = studentList.Where(isStudentTeenAger).AsQueryable().Select(outDto);
            //AsQueryable  注意这里,select(Expression) 需要我们是IQueryable
            //IEnumerable 使用的是 Func
            //IQueryable  使用的是  Expression