直接上代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; namespace ConsoleApplication1 { public static class Extensions { public static Predicate<T> ToPredicate<T>(this Func<T, bool> source) { Predicate<T> result = new Predicate<T>(source); return result; } } public class LambdaExtention<T> { /// <summary> /// 表达式集合 /// </summary> private List<Expression> m_lstExpression = null; /// <summary> /// 参数 /// </summary> private ParameterExpression m_Parameter = null; /// <summary> /// 构造函数 /// </summary> public LambdaExtention() { m_lstExpression = new List<Expression>(); m_Parameter = Expression.Parameter(typeof(T), "x"); } /// <summary> /// 字符串Contains筛选 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> public void Contains(Expression<Func<T, object>> expProperty, object objValue) { Expression expRes = Expression.Call(GetMemberExpression(expProperty), typeof(string).GetMethod("Contains"), Expression.Constant(objValue)); m_lstExpression.Add(expRes); } /// <summary> /// 等于 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> public void Equal(Expression<Func<T, object>> expProperty, object objValue) { var member = GetMemberExpression(expProperty); Expression expRes = Expression.Equal(member, Expression.Constant(objValue, member.Type)); m_lstExpression.Add(expRes); } /// <summary> /// 等于 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> public void Equal(string strProperty, object objValue) { var member = GetMemberExpression(strProperty); Expression expRes = Expression.Equal(member, Expression.Constant(objValue, member.Type)); m_lstExpression.Add(expRes); } /// <summary> /// 小于 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> public void LessThan(Expression<Func<T, object>> expProperty, object objValue) { var member = GetMemberExpression(expProperty); Expression expRes = Expression.LessThan(member, Expression.Constant(objValue, member.Type)); m_lstExpression.Add(expRes); } /// <summary> /// 小于等于 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> public void LessThanOrEqual(Expression<Func<T, object>> expProperty, object objValue) { var member = GetMemberExpression(expProperty); Expression expRes = Expression.LessThanOrEqual(member, Expression.Constant(objValue, member.Type)); m_lstExpression.Add(expRes); } /// <summary> /// 大于 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> public void GreaterThan(Expression<Func<T, object>> expProperty, object objValue) { var member = GetMemberExpression(expProperty); Expression expRes = Expression.GreaterThan(member, Expression.Constant(objValue, member.Type)); m_lstExpression.Add(expRes); } /// <summary> /// 大于等于 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> public void GreaterThanOrEqual(Expression<Func<T, object>> expProperty, object objValue) { var member = GetMemberExpression(expProperty); Expression expRes = Expression.GreaterThanOrEqual(member, Expression.Constant(objValue, member.Type)); m_lstExpression.Add(expRes); } /// <summary> /// 为真方法 /// </summary> /// <param name="expProperty">表达式属性</param> /// <param name="objValue">值</param> /// <param name="typeName">类名名称</param> /// <param name="methodName">方法名称</param> public void IsTureMethod(Expression<Func<T, object>> expProperty, object objValue, string typeName, string methodName) { Expression expRes = Expression.IsTrue(GetMemberExpression(expProperty), Type.GetType(typeName).GetMethod(methodName) ); m_lstExpression.Add(expRes); } /// <summary> /// 获得Lambda /// </summary> /// <returns>返回Lambda</returns> private LambdaExpression GetLambda() { Expression whereExpr = null; foreach (var expr in this.m_lstExpression) { if (whereExpr == null) { whereExpr = expr; } else { whereExpr = Expression.And(whereExpr, expr); } } if (whereExpr == null) { return null; } return Expression.Lambda(whereExpr, m_Parameter); } /// <summary> /// 获得谓词 /// </summary> /// <returns>返回谓词</returns> public Predicate<T> GetPredicate() { Func<T, bool> match = (Func<T, bool>)GetLambda().Compile(); return match.ToPredicate(); } /// <summary> /// 获得方法 /// </summary> /// <returns>返回方法</returns> public Func<T, bool> GetFunc() { Func<T, bool> match = (Func<T, bool>)GetLambda().Compile(); return match; } /// <summary> /// 获得MemberExpression /// </summary> /// <param name="exp">表达式属性</param> /// <returns>返回MemberExpression</returns> private MemberExpression GetMemberExpression(Expression<Func<T, object>> exp) { var arrSplit = exp.Body.ToString().Split("(.)".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var strProperty = string.Join(".", arrSplit, 1, arrSplit.Length - 1); MemberExpression member = GetMemberExpression(strProperty); return member; } /// <summary> /// 获得MemberExpression /// </summary> /// <param name="strProperty">表达式属性</param> /// <returns>返回MemberExpression</returns> private MemberExpression GetMemberExpression(string strProperty) { MemberExpression member; if (strProperty.Contains(‘.‘)) { string[] aryProperty = strProperty.Split(‘.‘); member = Expression.Property(m_Parameter, aryProperty[0]); for (int i = 1; i < aryProperty.Length; i++) { member = Expression.Property(member, member.Type, aryProperty[i]); } } else { member = Expression.Property(m_Parameter, strProperty); } return member; } } }
调用示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<Student> lstStu = new List<Student>(); lstStu.Add(new Student() { No = "001", Name = "zhangsan", Classes = new Classes() { ClassID = "002" } }); lstStu.Add(new Student() { No = "002", Name = "lisi", Classes = new Classes() { ClassID = "003" } }); lstStu.Add(new Student() { No = "003", Name = "wangwu", Classes = new Classes() { ClassID = "004" } }); lstStu.Add(new Student() { No = "004", Name = "maliu", Classes = new Classes() { ClassID = "005" } }); var le = new LambdaExtention<Student>(); ////自定义方法 le.IsTureMethod(x => x.Classes.ClassID, "0", "ConsoleApplication1.Test", "Show"); ////两种写法 le.Equal(x => x.Classes.ClassID, "002"); le.Equal("Classes.ClassID", "002"); List<Student> stu = lstStu.FindAll(le.GetPredicate()); foreach (var item in stu) { Console.WriteLine(item.No); } Console.Read(); } } static class Test { public static bool Show(string str) { return false; } } }
文章引用自:http://blog.jobbole.com/99431/
时间: 2024-10-29 08:58:49