规约模式的ef拼接
public class LamadaExtention<Dto> where Dto : new() { private List<Expression> m_lstExpression = null; private ParameterExpression m_Parameter = null; public LamadaExtention() { m_lstExpression = new List<Expression>(); m_Parameter = Expression.Parameter(typeof(Dto), "x"); } //构造表达式,存放到m_lstExpression集合里面 public void GetExpression(string strPropertyName, object strValue, ExpressionType expressType) { Expression expRes = null; MemberExpression member = Expression.PropertyOrField(m_Parameter, strPropertyName); if (expressType == ExpressionType.Contains) { expRes = Expression.Call(member, typeof(string).GetMethod("Contains"), Expression.Constant(strValue)); } else if (expressType == ExpressionType.Equal) { expRes = Expression.Equal(member, Expression.Constant(strValue, member.Type)); } else if (expressType == ExpressionType.LessThan) { expRes = Expression.LessThan(member, Expression.Constant(strValue, member.Type)); } else if (expressType == ExpressionType.LessThanOrEqual) { expRes = Expression.LessThanOrEqual(member, Expression.Constant(strValue, member.Type)); } else if (expressType == ExpressionType.GreaterThan) { expRes = Expression.GreaterThan(member, Expression.Constant(strValue, member.Type)); } else if (expressType == ExpressionType.GreaterThanOrEqual) { expRes = Expression.GreaterThanOrEqual(member, Expression.Constant(strValue, member.Type)); } //return expRes; m_lstExpression.Add(expRes); } //针对Or条件的表达式 public void GetExpression(string strPropertyName, List<object> lstValue) { Expression expRes = null; MemberExpression member = Expression.PropertyOrField(m_Parameter, strPropertyName); foreach (var oValue in lstValue) { if (expRes == null) { expRes = Expression.Equal(member, Expression.Constant(oValue, member.Type)); } else { expRes = Expression.Or(expRes, Expression.Equal(member, Expression.Constant(oValue, member.Type))); } } m_lstExpression.Add(expRes); } //多个字段or同一个值 public void GetExpression(List<string> listStrPropertyName, object strValue, ExpressionType expressType) { Expression expRes = null; foreach (var itemValue in listStrPropertyName) { MemberExpression member = Expression.PropertyOrField(m_Parameter, itemValue); if (expressType == ExpressionType.Contains) { if (expRes == null) { expRes = Expression.Call(member, typeof(string).GetMethod("Contains"), Expression.Constant(strValue)); //expRes = Expression.Equal(member, Expression.Constant(strValue, member.Type)); } else { expRes = Expression.Or(expRes, Expression.Call(member, typeof(string).GetMethod("Contains"), Expression.Constant(strValue))); } } else { if (expRes == null) { expRes = Expression.Equal(member, Expression.Constant(strValue, member.Type)); } else { expRes = Expression.Or(expRes, Expression.Equal(member, Expression.Constant(strValue, member.Type))); } } } m_lstExpression.Add(expRes); } //得到Lamada表达式的Expression对象 public Expression<Func<Dto, bool>> 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<Func<Dto, Boolean>>(whereExpr, m_Parameter); } } //用于区分操作的枚举 public enum ExpressionType { Contains,//like Equal,//等于 LessThan,//小于 LessThanOrEqual,//小于等于 GreaterThan,//大于 GreaterThanOrEqual//大于等于 }
使用:
var oLamadaExtention = new LamadaExtention<CommonCase>(); //2.依次构造Lamada表达式 if (!string.IsNullOrEmpty(key)) { List<string> lstValue = new List<string>(); lstValue.Add("Title"); lstValue.Add("KeyWord"); oLamadaExtention.GetExpression(lstValue, key,ExpressionType.Contains); } if (!string.IsNullOrEmpty(createSTime)) { var time = Convert.ToDateTime(createSTime); oLamadaExtention.GetExpression("CreateTime", time, ExpressionType.GreaterThanOrEqual); } if (!string.IsNullOrEmpty(createETime)) { var time = Convert.ToDateTime(createETime); oLamadaExtention.GetExpression("CreateTime", time, ExpressionType.LessThanOrEqual); }
积累小的知识,才能成就大的智慧,希望网上少一些复制多一些原创有用的答案