private static Expression GetCompareExpression(FilterInstruction pInstruction, Expression property) { if (!pInstruction.MatchExact) { if (pInstruction.Value != null && pInstruction.PropertyInfo.PropertyType == typeof(string)) { var value = Expression.Constant(((string)pInstruction.Value).Trim().ToLowerInvariant()); var exprToLower = Expression.Call(property, StringToLower); return(Expression.Condition( Expression.Equal(property, Expression.Constant(null)), Expression.Constant(false), Expression.Call(exprToLower, StringContains, value) )); } if (pInstruction.Value != null && (pInstruction.PropertyInfo.PropertyType == typeof(DateTime) || pInstruction.PropertyInfo.PropertyType == typeof(DateTime?))) { DateTime value = (DateTime)pInstruction.Value; var day = Expression.Convert(Expression.Constant(value.Date), pInstruction.PropertyInfo.PropertyType); var nextDay = Expression.Convert(Expression.Constant(value.Date.Add(TimeSpan.FromDays(1))), pInstruction.PropertyInfo.PropertyType); return(Expression.AndAlso(Expression.GreaterThanOrEqual(property, day), Expression.LessThan(property, nextDay))); } } return(Expression.Equal(property, Expression.Constant(pInstruction.Value))); }
private static IQueryable <TSource> PerformOperation(IQueryable <TSource> source, FilterInstruction pInstruction, MethodInfo pMethodInfo) { var param = Expression.Parameter(typeof(TSource), "p"); Expression propertyExpr = pInstruction.PropertyInfo.GetMemberExpression(param); Expression exprEqual = GetCompareExpression(pInstruction, propertyExpr); var lambda = Expression.Lambda(exprEqual, param); var method = pMethodInfo.MakeGenericMethod(new[] { typeof(TSource) }); var ret = method.Invoke(null, new object[] { source, lambda }); return((IQueryable <TSource>)ret); }