/// <summary> /// 转换Filter数组为表达式 /// </summary> /// <param name="filters"></param> /// <param name="param"></param> /// <param name="operation"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> private static Expression ConvertFilters <T>(Filter[] filters, ParameterExpression param, string operation) { if (filters == null || !filters.Any()) { return(null); } Expression result = param.GenerateBody <T>(filters[0]); if (filters.Length == 1) { return(result); } if (operation == "and") { foreach (var filter in filters.Skip(1)) { result = result.AndAlso(param.GenerateBody <T>(filter)); } } else { foreach (var filter in filters.Skip(1)) { result = result.Or(param.GenerateBody <T>(filter)); } } return(result); }
/// <summary> /// 转换filtergroup为表达式 /// </summary> /// <param name="filterGroup"></param> /// <param name="param"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static Expression ConvertGroup <T>(FilterGroup filterGroup, ParameterExpression param) { if (filterGroup == null) { return(null); } if (filterGroup.Filters.Length == 1 && !filterGroup.Children.Any()) //只有一个条件 { return(param.GenerateBody <T>(filterGroup.Filters[0])); } Expression result = ConvertFilters <T>(filterGroup.Filters, param, filterGroup.Operation); Expression gresult = ConvertGroup <T>(filterGroup.Children, param, filterGroup.Operation); if (gresult == null) { return(result); } if (result == null) { return(gresult); } if (filterGroup.Operation == "and") { return(result.AndAlso(gresult)); } else //or { return(result.Or(gresult)); } }
/// <summary>处理优先级 /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="param"></param> /// <param name="lstFirstNodes"></param> /// <returns></returns> public Expression ConvertToFirstLevelFiltersByTreeGridView <T>(ParameterExpression param, List <TreeGridNode> lstFirstNodes) { Expression expressAll = null; List <Filter> filtersFirst = ConvertFiltersByTreeGridView(tvTaskList, lstFirstNodes); for (int i = 0; i < filtersFirst.Count; i++) { Expression express = param.GenerateBody <T>(filtersFirst[i]); if (i == 0) { expressAll = express; continue; } if (filtersFirst[i].AndOr == "并且") { if (expressAll != null) { expressAll = Expression.And(expressAll, express); } } else if (filtersFirst[i].AndOr == "或者") { if (expressAll != null) { expressAll = Expression.Or(expressAll, express); //这里可以根据需要自由组合 } } } return(expressAll); }
public static Expression <Func <T, bool> > GenerateTypeBody <T>(this ParameterExpression param, Filter filterObj) { return((Expression <Func <T, bool> >)(param.GenerateBody <T>(filterObj))); }
/// <summary> /// 转换filterlist为表达式 /// </summary> /// <param name="filterList"></param> /// <param name="param"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static Expression ConvertList <T>(List <FilterSo> filterList, ParameterExpression param) { if (filterList == null) { return(null); } Expression result = null; Expression gresult = null; PropertyInfo property; Expression left = null; foreach (var item in filterList) { switch (item.mode) { case "condition": gresult = param.GenerateBody <T>(item); if (gresult == null) { break; } if (item.prefix == "or") { if (result != null) { result = result.Or(gresult); } else { result = gresult; } } else { if (result != null) { result = result.AndAlso(gresult); } else { result = gresult; } } break; case "group": gresult = ConvertList <T>(item.children, param); if (gresult == null) { break; } if (item.prefix == "or") { if (result != null) { result = result.Or(gresult); } else { result = gresult; } } else { if (result != null) { result = result.AndAlso(gresult); } else { result = gresult; } } break; case "in": property = typeof(T).GetProperty(item.field); left = Expression.Property(param, property); if (item.values == null || item.values.Count == 0) { break; } if (property.PropertyType == typeof(int)) { List <int> list = new List <int>(); foreach (var temp in item.values) { list.Add(int.Parse(temp)); } gresult = Expression.Call(Expression.Constant(list), typeof(List <int>).GetMethod("Contains", new Type[] { typeof(int) }), left); } if (property.PropertyType == typeof(Nullable <int>)) { List <int?> list = new List <int?>(); foreach (var temp in item.values) { list.Add(int.Parse(temp)); } gresult = Expression.Call(Expression.Constant(list), typeof(List <int?>).GetMethod("Contains", new Type[] { typeof(int?) }), left); } else if (property.PropertyType == typeof(DateTime)) { List <DateTime> list = new List <DateTime>(); foreach (var temp in item.values) { list.Add(DateTime.Parse(temp)); } gresult = Expression.Call(Expression.Constant(list), typeof(List <DateTime>).GetMethod("Contains", new Type[] { typeof(DateTime) }), left); } else if (property.PropertyType == typeof(Nullable <DateTime>)) { List <DateTime?> list = new List <DateTime?>(); foreach (var temp in item.values) { list.Add(DateTime.Parse(temp)); } gresult = Expression.Call(Expression.Constant(list), typeof(List <DateTime?>).GetMethod("Contains", new Type[] { typeof(DateTime?) }), left); } else if (property.PropertyType == typeof(decimal)) { List <decimal> list = new List <decimal>(); foreach (var temp in item.values) { list.Add(decimal.Parse(temp)); } gresult = Expression.Call(Expression.Constant(list), typeof(List <decimal>).GetMethod("Contains", new Type[] { typeof(decimal) }), left); } else if (property.PropertyType == typeof(Nullable <decimal>)) { List <decimal?> list = new List <decimal?>(); foreach (var temp in item.values) { list.Add(decimal.Parse(temp)); } gresult = Expression.Call(Expression.Constant(list), typeof(List <decimal?>).GetMethod("Contains", new Type[] { typeof(decimal?) }), left); } else if (property.PropertyType == typeof(bool)) { List <bool> list = new List <bool>(); foreach (var temp in item.values) { if (temp == "1") { list.Add(true); } else { list.Add(false); } } gresult = Expression.Call(Expression.Constant(list), typeof(List <bool>).GetMethod("Contains", new Type[] { typeof(bool) }), left); } else if (property.PropertyType == typeof(Nullable <bool>)) { List <bool?> list = new List <bool?>(); foreach (var temp in item.values) { if (temp == "1") { list.Add(true); } else { list.Add(false); } } gresult = Expression.Call(Expression.Constant(list), typeof(List <bool?>).GetMethod("Contains", new Type[] { typeof(bool?) }), left); } else { gresult = Expression.Call(Expression.Constant(item.values), typeof(List <string>).GetMethod("Contains", new Type[] { typeof(string) }), left); } if (result != null) { result = result.AndAlso(gresult); } else { result = gresult; } break; case "date": property = typeof(T).GetProperty(item.field); left = Expression.Property(param, property); bool isNull = false; if (property.PropertyType == typeof(Nullable <DateTime>)) { isNull = true; } DateTime?startTime = null; DateTime?endTime = null; switch (item.type) { case "yesterday": startTime = DateTime.Now.Date.AddDays(-1); endTime = DateTime.Now.Date; break; case "thisWeek": startTime = DateTime.Now.Date.AddDays(1 - Convert.ToInt32(DateTime.Now.Date.DayOfWeek.ToString("d"))); //本周周一 endTime = ((DateTime)startTime).AddDays(7); //本周周日 break; case "lastWeek": startTime = DateTime.Now.Date.AddDays(1 - Convert.ToInt32(DateTime.Now.Date.DayOfWeek.ToString("d")) - 7); //上周周一 endTime = ((DateTime)startTime).AddDays(7); //上周周日 break; case "thisMonth": startTime = DateTime.Now.Date.AddDays(1 - DateTime.Now.Date.Day); //本月月初 endTime = ((DateTime)startTime).AddMonths(1); //本月月末 break; case "thisYear": startTime = new DateTime(DateTime.Now.Date.Year, 1, 1); //本年年初 endTime = new DateTime(DateTime.Now.Date.AddYears(1).Year, 1, 1); //本年年初 break; case "specific": var tempTime = DateTime.Parse(item.value); startTime = tempTime.Date; endTime = tempTime.Date.AddDays(+1); break; default: break; } if (startTime != null && endTime != null) { if (isNull) { gresult = Expression.GreaterThanOrEqual(left, Expression.Constant(startTime, typeof(DateTime?))); gresult = gresult.And(Expression.LessThan(left, Expression.Constant(endTime, typeof(DateTime?)))); } else { gresult = Expression.GreaterThanOrEqual(left, Expression.Constant((DateTime)startTime)); gresult = gresult.And(Expression.LessThan(left, Expression.Constant((DateTime)endTime))); } } if (result != null) { result = result.AndAlso(gresult); } else { result = gresult; } break; default: break; } } return(result); }
public static Expression <Func <T, bool> > GetExpressions(T t) { ParameterExpression u = Expression.Parameter(typeof(T), "u"); Filter filterObj = new Filter(); filterObj.Key = ""; filterObj.Value = ""; // 获得此模型的类型 Type type = typeof(T); Expression result = Expression.Constant(true); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); for (int i = 0; i < propertys.Length; i++) { Object value2 = propertys[i].GetValue(t, null); if (value2 != null && value2.ToString() != "") { filterObj.Key = propertys[i].Name.ToString(); if (propertys[i].PropertyType == typeof(DateTime) || propertys[i].PropertyType == typeof(DateTime?)) { filterObj.Value = ((DateTime)propertys[i].GetValue(t, null)).ToString("yyyy-MM-dd HH:mm:ss.fff"); } else { filterObj.Value = propertys[i].GetValue(t, null).ToString(); } if ( (propertys[i].PropertyType == typeof(int) || propertys[i].PropertyType == typeof(int?)) || (propertys[i].PropertyType == typeof(short) || propertys[i].PropertyType == typeof(short?)) || (propertys[i].PropertyType == typeof(long) || propertys[i].PropertyType == typeof(long?)) || (propertys[i].PropertyType == typeof(decimal) || propertys[i].PropertyType == typeof(decimal?)) || (propertys[i].PropertyType == typeof(double) || propertys[i].PropertyType == typeof(double?)) || (propertys[i].PropertyType == typeof(bool) || propertys[i].PropertyType == typeof(bool?)) ) { filterObj.Contrast = ConvertOperString("eq"); filterObj.JqContrast = "eq"; if (propertys[i].PropertyType == typeof(int?) || propertys[i].PropertyType == typeof(short?) || propertys[i].PropertyType == typeof(long?) || propertys[i].PropertyType == typeof(double?) || propertys[i].PropertyType == typeof(bool?) ) { PropertyInfo property = typeof(T).GetProperty(filterObj.Key); Expression left = Expression.Property(u, property); try { result = result.AndAlso(Expression.Property(left, "HasValue")); } catch (Exception ex) { throw ex; } } } else { filterObj.Contrast = ConvertOperString("cn"); filterObj.JqContrast = "cn"; } result = result.AndAlso(u.GenerateBody <T>(filterObj)); } } Expression <Func <T, bool> > expression = u.GenerateTypeLambda <T>(result); return(expression); }