C# Lambda 表达式学习之(二):LambdaExpression 实战练习
2012-06-12 12:16 音乐让我说 阅读(700) 评论(0) 编辑 收藏 举报可能你还感兴趣:
1. C# Lambda 表达式学习之(一):得到一个类的字段(Field)或属性(Property)名,强类型得到
2. C# Lambda 表达式学习之(二):LambdaExpression 实战练习
3. 基于 Entity Framework FluentAPI 思想的强类型的 Lambda Expressions 实体映射
4. C# Lambda 表达式学习之(三):动态构建类似于 c => c.Age == null || c.Age > 18 的表达式
5. C# Lambda 表达式学习之(四):动态构建类似于 c => c.Age == 2 || c.Age == 5 || c => c.Age == 17 等等一个或多个 OrElse 的表达式
转载自:http://www.cnblogs.com/jax/archive/2010/03/15/1686594.html
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; namespace DearBruce.ConAppTest { /// <summary> /// 转载自:http://www.cnblogs.com/jax/archive/2010/03/15/1686594.html /// </summary> public class LambdaExpressionTest { public static void Test() { TestSimple(); Test1(); Test2(); Test3(); } /// <summary> /// 简单类型 int - 找出 list 集合中等于常量 1 的值 /// </summary> private static void TestSimple() { List<int> list = new List<int>() { 1, 2, 3, 4 }; ParameterExpression parameter = Expression.Parameter(typeof(int), "p"); ExpressionType op = ExpressionType.Equal; ConstantExpression para2 = Expression.Constant(1); BinaryExpression result2 = Expression.MakeBinary(op, parameter, para2); Expression<Func<int, bool>> expression = Expression.Lambda(result2, parameter) as Expression<Func<int, bool>>; IEnumerable<int> newList = list.Where(expression.Compile()); Console.WriteLine("简单调用结果:\n"); CommonHelper.Show(newList); } /// <summary> /// 复杂类型 - 判断集合中,实体中 A1 属性大于 1 的集合 /// </summary> private static void Test1() { List<ClassA> list = new List<ClassA>(); list.Add(new ClassA() { A1 = 1, A2 = "2" }); list.Add(new ClassA() { A1 = 11, A2 = "22" }); ParameterExpression parameter = Expression.Parameter(typeof(ClassA), "p"); MemberExpression left = Expression.PropertyOrField(parameter, "A1"); ExpressionType op = ExpressionType.GreaterThan; ConstantExpression right = Expression.Constant(1); BinaryExpression result = Expression.MakeBinary(op, left, right); Expression<Func<ClassA, bool>> expression = Expression.Lambda(result, parameter) as Expression<Func<ClassA, bool>>; IEnumerable<ClassA> newList = list.Where(expression.Compile()); Console.WriteLine("复杂类型调用结果:\n"); CommonHelper.Show<ClassA>(newList, e => "A1: " + e.A1 + ", A2: " + e.A2); } /// <summary> /// 拼接2个条件 /// </summary> private static void Test2() { List<ClassA> list = new List<ClassA>(); list.Add(new ClassA() { A1 = 1, A2 = "2" }); list.Add(new ClassA() { A1 = 11, A2 = "22" }); list.Add(new ClassA() { A1 = 122, A2 = "122" }); ParameterExpression parameter = Expression.Parameter(typeof(ClassA), "p"); MemberExpression left = Expression.PropertyOrField(parameter, "A1"); ExpressionType op = ExpressionType.GreaterThan; ConstantExpression right = Expression.Constant(1); BinaryExpression result = Expression.MakeBinary(op, left, right); right = Expression.Constant(100); op = ExpressionType.LessThan; var result2 = Expression.MakeBinary(op, left, right); ExpressionType et = ExpressionType.And; BinaryExpression newResult = Expression.MakeBinary(et, result, result2); Expression<Func<ClassA, bool>> expression = Expression.Lambda(newResult, parameter) as Expression<Func<ClassA, bool>>; IEnumerable<ClassA> newList = list.Where(expression.Compile()); Console.WriteLine("拼接2个条件调用结果:\n"); CommonHelper.Show<ClassA>(newList, e => "A1: " + e.A1 + ", A2: " + e.A2); } /// <summary> /// 调用自定义方法 /// </summary> private static void Test3() { List<ClassA> list = new List<ClassA>(); list.Add(new ClassA() { A1 = 11, A2 = "2" }); list.Add(new ClassA() { A1 = 11, A2 = "22" }); list.Add(new ClassA() { A1 = 122, A2 = "122" }); ParameterExpression parameter = Expression.Parameter(typeof(ClassA), "p"); MemberExpression para1 = Expression.PropertyOrField(parameter, "A2"); MethodCallExpression expCall = Expression.Call(parameter, typeof(ClassA).GetMethod("TestMethod"), para1); ExpressionType op = ExpressionType.Equal; ConstantExpression para2 = Expression.Constant(true); BinaryExpression result2 = Expression.MakeBinary(op, expCall, para2); Expression<Func<ClassA, bool>> expression = Expression.Lambda(result2, parameter) as Expression<Func<ClassA, bool>>; IEnumerable<ClassA> newList = list.Where(expression.Compile()); Console.WriteLine("自定义方法调用结果:\n"); CommonHelper.Show<ClassA>(newList, e => "A1: " + e.A1 + ", A2: " + e.A2); } } public class ClassA { public int A1 { get; set; } public string A2 { get; set; } public bool TestMethod(string pa) { return pa == "22"; } } }
LambdaExpressionTest 调用结果截图:
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。