public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if (model.LoginAccount != null) { Temp = Temp.Where(X => X.LoginAccount == model.LoginAccount); } if (model.ShopName != null) { Temp = Temp.Where(X => X.ShopName == model.ShopName); } return JsonConvert.SerializeObject(Temp.ToList(), Formatting.Indented, new JsonSerializerSettings() { DateFormatHandling = 0 }); }
这是MVC 项目。 点击查询 执行 GetGridJSON 方法。由于 MVC 规范 name属性可以匹配 模型,如果 不填写 账号 、名称 ,返回的 model 里面 的 账号 、名称属性为 null。 每次都要判断 是否 是 null ,如果是 ,就 查询 所有,否 才能匹配where。
这很不好,如果我的条件 很多 呢? 这样的判断 就 很烦了。
我做了如下如下封装
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Linq.Expressions; namespace EF_DAL { /// <summary> /// 动态生成 表达式树 /// </summary> public class Custom_Expression { public delegate Expression delegate_Expression(Expression left, Expression right); private static BinaryExpression filter_Temp = Expression.Equal(Expression.Constant(1), Expression.Constant(1)); /// <summary> /// 自定义Equal方法(允许value为null),value为null 的时候,该查询条件 不会生成 /// </summary> /// <typeparam name="T">实体数据类型</typeparam> /// <param name="StrArrPropertyName">以逗号分割的列名称</param> /// <param name="values">这些列对应的值</param> /// <returns>返回Lambda表达式,eg:where(Lambda)</returns> public Expression<Func<T, bool>> Custom_Equal<T>(string StrArrPropertyName, params object[] values) { return Custom_Expression_Function<T>(Equal_result, StrArrPropertyName, values); } private void Init() { filter_Temp = Expression.Equal(Expression.Constant(1), Expression.Constant(1)); } public Expression<Func<T, bool>> Custom_Contain<T>(string StrArrPropertyName, params object[] values) { return Custom_Expression_Function<T>(Call_result, StrArrPropertyName, values); } private Expression Call_result(Expression left, Expression right) { return Expression.Call(left, typeof(string).GetMethod("Contains"), right); } private Expression Equal_result(Expression left, Expression right) { return Expression.Equal(left, right); } private Expression<Func<T, bool>> Custom_Expression_Function<T>(delegate_Expression del, string StrArrPropertyName, params object[] values) { Init(); var PropertyName_arr = StrArrPropertyName.Split(‘,‘); var param = Expression.Parameter(typeof(T)); for (int i = 0; i < PropertyName_arr.Length; i++) { if (values[i] == null) continue; string propertyName = PropertyName_arr[i].ToString(); var value = values[i]; Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName)); Expression right = Expression.Constant(value, value.GetType()); Expression result = del(left, right); filter_Temp = Expression.And(filter_Temp, result); } return Expression.Lambda<Func<T, bool>>(filter_Temp, param); } } }
直接这样 调用啦!
public string GetGridJSON(TraderInfo model) { EF_DAL.Custom_Expression CE = new EF_DAL.Custom_Expression(); var rrr = db.TraderInfo.Where(CE.Custom_Equal<TraderInfo>("LoginAccount,ShopName", model.LoginAccount, model.ShopName)).ToList(); return JsonConvert.SerializeObject(rrr, Formatting.Indented, new JsonSerializerSettings() { DateFormatHandling = 0 }); }
期待更好的方法。
时间: 2024-10-21 19:11:28