/// <summary> /// Creates a filter expression from a filter group /// </summary> /// <typeparam name="T"></typeparam> /// <param name="filterGroup"></param> /// <param name="parameterName"></param> /// <returns></returns> public static Expression <Func <T, bool> > GetFilterExpression <T>(this FilterGroup filterGroup, string parameterName = "f") where T : class { if (filterGroup == null || ((filterGroup.FilterGroups == null || filterGroup.FilterGroups.Count() == 0) && (filterGroup.Filters == null || filterGroup.Filters.Count() == 0))) { return(null); } List <Filter> filters = filterGroup.GetAll(); List <object> values = filters.Select(f => { MemberInfo pInfo = typeof(T).GetMemberInfoFromFullName(f.Field); if (f.Value != null && pInfo.GetMemberType() != f.Value.GetType()) //convert values if necessary { f.Value = Convert.ChangeType(f.Value, pInfo.GetMemberType()); //NewtonSoft Json defaults to long for whole numbers and double for fractions } return(f.Value); }).ToList(); return(System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda ( false, new ParameterExpression[] { Expression.Parameter(typeof(T), parameterName) }, typeof(bool), filterGroup.ToExpression <T>(filters, parameterName, values, false), values.ToArray() ) as Expression <Func <T, bool> >); }
public static List <Filter> GetAll(this FilterGroup filterGroup) { if (filterGroup == null) { return(null); } List <Filter> filters = new List <Filter>(); filterGroup.GetAll(filters); return(filters); }