using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Linq.Expressions; namespace ExpressionToSql { public class LambdaHelper { /// <summary> /// Id等于 /// </summary> /// <param name="Id"></param> /// <returns></returns> public Func<Model, bool> CreateFilterID(int Id) { ParameterExpression obj = Expression.Parameter(typeof(Model), "obj"); ConstantExpression constant = Expression.Constant(Id); MemberExpression member = Expression.PropertyOrField(obj, "Id"); BinaryExpression query = Expression.Equal(member, constant); var lambda = Expression.Lambda<Func<Model, bool>>(query, obj); var func = lambda.Compile(); return func; } /// <summary> /// Model等于 /// </summary> /// <param name="model"></param> /// <returns></returns> public Func<Model, bool> CreateFilterModel(Model model) { ParameterExpression obj = Expression.Parameter(typeof(Model), "obj"); ConstantExpression constant = Expression.Constant(model.Id); MemberExpression member = Expression.PropertyOrField(obj, "Id"); BinaryExpression query = Expression.Equal(member, constant); var lambda = Expression.Lambda<Func<Model, bool>>(query, obj); var func = lambda.Compile(); return func; } /// <summary> /// Object 等于(1参数) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public Func<T, bool> CreateFilterObject<T>(T model) where T : class,new() { ParameterExpression obj = Expression.Parameter(typeof(T), "obj"); var constVal = Convert.ToInt32(GetPropertyValue(model, "Id")); ConstantExpression constant = Expression.Constant(constVal); MemberExpression member = Expression.PropertyOrField(obj, "Id"); BinaryExpression query = Expression.Equal(member, constant); var lambda = Expression.Lambda<Func<T, bool>>(query, obj); var func = lambda.Compile(); return func; } /// <summary> /// Model等于(2参数) /// </summary> /// <returns></returns> public Func<Model, Model, bool> CreateModelEqual() { ParameterExpression obj = Expression.Parameter(typeof(Model), "obj"); ParameterExpression obj1 = Expression.Parameter(typeof(Model), "obj1"); MemberExpression member = Expression.PropertyOrField(obj, "Id"); MemberExpression member1 = Expression.PropertyOrField(obj1, "Id"); BinaryExpression query = Expression.Equal(member, member1); var lambda = Expression.Lambda<Func<Model, Model, bool>>(query, obj, obj1); var func = lambda.Compile(); return func; } /// <summary> /// Int 等于 /// </summary> /// <returns></returns> public Func<int, int, bool> CreateIntEqual() { ParameterExpression member = Expression.Parameter(typeof(int), "id"); ParameterExpression val = Expression.Parameter(typeof(int), "val"); BinaryExpression bin = Expression.Equal(member, val); var lambda = Expression.Lambda<Func<int, int, bool>>(bin, member, val); var func = lambda.Compile(); return func; } #region Extend private object GetPropertyValue<T>(T model, string proertyName) where T : class { return model.GetType().GetProperty(proertyName).GetValue(model); } #endregion } public class Model { public int Id { get; set; } public string Name { get; set; } } }
static void TestLambda() { LambdaHelper helper = new LambdaHelper(); Model test1 = new Model() { Id = 1, Name = "lcc1" }; Model test2 = new Model() { Id = 2, Name = "lcc2" }; Model test3 = new Model() { Id = 3, Name = "lcc3" }; List<Model> list = new List<Model>() { test1, test2, test3 }; //CreateFilterID Console.WriteLine("-----------------CreateFilterID--------------------------"); var filterFunc = helper.CreateFilterID(1); Console.WriteLine("test1中的Id 等于1:" + filterFunc(test1)); Console.WriteLine("test2中的Id 等于1:" + filterFunc(test1)); //CreateFilterModel Console.WriteLine("--------------------CreateFilterModel---------------------"); var filterModelFunc = helper.CreateFilterModel(test1); Console.WriteLine("test1中的等于test1:" + filterModelFunc(test1)); Console.WriteLine("test1中的等于test2:" + filterModelFunc(test2)); Console.WriteLine("list中的等于test1的数量:" + list.Where(filterModelFunc).Count()); //CreateFilterObject Console.WriteLine("----------------------CreateFilterObject--------------------"); var filterObjectlFunc = helper.CreateFilterObject(test2); Console.WriteLine("test2中的等于test1:" + filterObjectlFunc(test1)); Console.WriteLine("test2中的等于test2:" + filterObjectlFunc(test2)); Console.WriteLine("list中的等于test2的数量:" + list.Where(filterObjectlFunc).Count()); //CreateModelEqual Console.WriteLine("-------------------CreateModelEqual--------------------------"); var modelEqualFunc = helper.CreateModelEqual(); Console.WriteLine("test1,test2是否相等:" + modelEqualFunc(test1, test2)); Console.WriteLine("test1,test1是否相等:" + modelEqualFunc(test1, test2)); //CreateIntEqual Console.WriteLine("-----------------CreateIntEqual------------------------------"); var intEqualFunc = helper.CreateIntEqual(); Console.WriteLine("1,2是否相等:" + intEqualFunc(1, 2)); Console.WriteLine("1,1是否相等:" + intEqualFunc(1, 1)); Console.Read(); }