private static void GetSelection(IFilterData data, out ParameterExpression parameterExp, out Expression propertyExp) { var childTree = FilterDataHelper.CreateChildTree(data); var parentData = GetParent(childTree); parameterExp = Expression.Variable(parentData.PropertyType); propertyExp = CreatePropertyOrSelect(childTree, parameterExp, data); }
private static void CreateParameters(IFilterData data, object value, out ParameterExpression parameterExp, out ConstantExpression constExp, out Expression propertyExp) { var childTree = FilterDataHelper.CreateChildTree(data); var parentData = GetParent(childTree); parameterExp = Expression.Variable(parentData.PropertyType); constExp = Expression.Constant(value); propertyExp = CreatePropertyExp(childTree, parameterExp); }
private static Expression CreateSelect(Expression parentExp, IFilterData propertyInfo, IFilterData data) { var newData = data.Copy(); var inner = FilterDataHelper.ChangeInnerParent(propertyInfo.Parent, newData); var innerLambda = PropertySelector(newData); var collectionType = parentExp.Type.GetInterface(nameof(IQueryable)) != null? typeof(Queryable) : parentExp.Type.GetInterface(nameof(IEnumerable)) != null ? typeof(Enumerable) : throw new ArgumentException(parentExp.Type.Name); var select = collectionType.GetMethods(BindingFlags.Static | BindingFlags.Public).First(m => m.Name == "Select" && m.GetParameters().Length == 2).MakeGenericMethod(new[] { inner.PropertyType, innerLambda.ReturnType }); return(Expression.Call(select, parentExp, innerLambda)); }
internal static LambdaExpression LikeStringPredicate(IFilterData data, string value) { CreateParameters(data, value, out var parameterExp, out var constExp, out var propertyExp); var propertyType = propertyExp.Type; Expression ConvertOrFalse <T>(bool isConvarted, T convarted) => !isConvarted?Expression.Constant(false) : (Expression)Expression.Equal(propertyExp, Expression.Constant(convarted, propertyExp.Type)); Expression body; if (propertyType.IsString()) { var containsInfo = typeof(DbFunctions).GetMethod(nameof(DbFunctions.Like), new[] { typeof(string), typeof(string), typeof(string) }); body = Expression.Call(null, containsInfo, propertyExp, ToLikeExp(value?.ToString() ?? ""), Expression.Constant(FilterDataHelper.LikeScreening.ToString())); } else if (propertyType == typeof(int) || propertyType == typeof(int?)) { body = ConvertOrFalse(int.TryParse(value, out int res), res); } else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?)) { body = ConvertOrFalse(DateTime.TryParse(value, out var res), res); } else if (propertyType == typeof(double) || propertyType == typeof(double?)) { body = ConvertOrFalse(double.TryParse(value, out var res), res); } else if (propertyType == typeof(decimal) || propertyType == typeof(decimal?)) { body = ConvertOrFalse(decimal.TryParse(value, out var res), res); } else if (propertyType == typeof(float) || propertyType == typeof(float?)) { body = ConvertOrFalse(float.TryParse(value, out var res), res); } else if (propertyType == typeof(byte) || propertyType == typeof(byte?)) { body = ConvertOrFalse(byte.TryParse(value, out var res), res); } else if (propertyType == typeof(long) || propertyType == typeof(long?)) { body = ConvertOrFalse(long.TryParse(value, out var res), res); } else if (propertyType == typeof(short) || propertyType == typeof(short?)) { body = ConvertOrFalse(short.TryParse(value, out var res), res); } else if (propertyType == typeof(bool) || propertyType == typeof(bool?)) { if (FilterDataHelper.IsTrue(value)) { body = Expression.Equal(propertyExp, Expression.Constant(true, propertyExp.Type)); } else if (FilterDataHelper.IsFalse(value)) { body = Expression.Equal(propertyExp, Expression.Constant(false, propertyExp.Type)); } else { body = ConvertOrFalse(bool.TryParse(value, out var res), res); } } else { body = Expression.Constant(false); } return(Expression.Lambda(body, parameterExp)); }
private static ConstantExpression ToLikeExp(string value) => Expression.Constant(FilterDataHelper.ToLikeString(value));