2018-03-12 22:55:20 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.DynamicType;
|
2018-04-10 21:08:27 -04:00
|
|
|
|
using PoweredSoft.DynamicLinq.Parser;
|
|
|
|
|
using PoweredSoft.DynamicLinq.Resolver;
|
2018-03-12 22:55:20 -04:00
|
|
|
|
using System;
|
2018-03-06 22:04:54 -05:00
|
|
|
|
using System.Collections;
|
2018-02-11 20:55:29 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
2018-02-12 21:28:53 -05:00
|
|
|
|
using System.Reflection;
|
2018-03-09 00:22:12 -05:00
|
|
|
|
|
2018-02-11 20:55:29 -05:00
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.DynamicLinq.Helpers
|
|
|
|
|
{
|
|
|
|
|
public static class QueryableHelpers
|
|
|
|
|
{
|
2018-03-13 22:01:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-06 20:43:49 -05:00
|
|
|
|
public static Expression GetConditionExpressionForMember(ParameterExpression parameter, Expression member, ConditionOperators conditionOperator, ConstantExpression constant, StringComparison? stringComparision)
|
2018-02-11 20:55:29 -05:00
|
|
|
|
{
|
|
|
|
|
if (parameter == null)
|
|
|
|
|
throw new ArgumentNullException("parameter");
|
|
|
|
|
|
|
|
|
|
if (member == null)
|
|
|
|
|
throw new ArgumentNullException("member");
|
|
|
|
|
|
|
|
|
|
if (constant == null)
|
|
|
|
|
throw new ArgumentNullException("constant");
|
|
|
|
|
|
2018-03-06 20:43:49 -05:00
|
|
|
|
|
|
|
|
|
Type stringType = typeof(string);
|
|
|
|
|
|
2018-02-11 20:55:29 -05:00
|
|
|
|
Expression ret = null;
|
|
|
|
|
|
|
|
|
|
if (conditionOperator == ConditionOperators.Equal)
|
2018-03-06 20:43:49 -05:00
|
|
|
|
{
|
|
|
|
|
if (member.Type == stringType && stringComparision.HasValue)
|
|
|
|
|
ret = Expression.Call(member, Constants.StringEqualWithComparisation, constant, Expression.Constant(stringComparision.Value));
|
|
|
|
|
else
|
|
|
|
|
ret = Expression.Equal(member, constant);
|
|
|
|
|
}
|
2018-02-12 22:27:09 -05:00
|
|
|
|
else if (conditionOperator == ConditionOperators.NotEqual)
|
2018-03-06 20:43:49 -05:00
|
|
|
|
{
|
|
|
|
|
if (member.Type == stringType && stringComparision.HasValue)
|
|
|
|
|
ret = Expression.Not(Expression.Call(member, Constants.StringEqualWithComparisation, constant, Expression.Constant(stringComparision.Value)));
|
|
|
|
|
else
|
|
|
|
|
ret = Expression.NotEqual(member, constant);
|
|
|
|
|
}
|
2018-02-11 20:55:29 -05:00
|
|
|
|
else if (conditionOperator == ConditionOperators.GreaterThan)
|
|
|
|
|
ret = Expression.GreaterThan(member, constant);
|
|
|
|
|
else if (conditionOperator == ConditionOperators.GreaterThanOrEqual)
|
|
|
|
|
ret = Expression.GreaterThanOrEqual(member, constant);
|
|
|
|
|
else if (conditionOperator == ConditionOperators.LessThan)
|
|
|
|
|
ret = Expression.LessThan(member, constant);
|
|
|
|
|
else if (conditionOperator == ConditionOperators.LessThanOrEqual)
|
|
|
|
|
ret = Expression.LessThanOrEqual(member, constant);
|
|
|
|
|
else if (conditionOperator == ConditionOperators.Contains)
|
2018-03-06 20:43:49 -05:00
|
|
|
|
{
|
|
|
|
|
if (member.Type == stringType && stringComparision.HasValue)
|
|
|
|
|
ret = Expression.GreaterThan(Expression.Call(member, Constants.IndexOfMethod, constant, Expression.Constant(stringComparision.Value)), Expression.Constant(-1));
|
|
|
|
|
else
|
|
|
|
|
ret = Expression.Call(member, Constants.ContainsMethod, constant);
|
|
|
|
|
}
|
2018-02-11 20:55:29 -05:00
|
|
|
|
else if (conditionOperator == ConditionOperators.StartsWith)
|
2018-03-06 20:43:49 -05:00
|
|
|
|
{
|
|
|
|
|
if (member.Type == stringType && stringComparision.HasValue)
|
|
|
|
|
ret = Expression.Call(member, Constants.StartsWithMethodWithComparisation, constant, Expression.Constant(stringComparision.Value));
|
|
|
|
|
else
|
|
|
|
|
ret = Expression.Call(member, Constants.StartsWithMethod, constant);
|
|
|
|
|
}
|
2018-02-11 20:55:29 -05:00
|
|
|
|
else if (conditionOperator == ConditionOperators.EndsWith)
|
2018-03-06 20:43:49 -05:00
|
|
|
|
{
|
|
|
|
|
if (member.Type == stringType && stringComparision.HasValue)
|
|
|
|
|
ret = Expression.Call(member, Constants.EndsWithMethodWithComparisation, constant, Expression.Constant(stringComparision.Value));
|
|
|
|
|
else
|
|
|
|
|
ret = Expression.Call(member, Constants.EndsWithMethod, constant);
|
|
|
|
|
}
|
2018-02-11 20:55:29 -05:00
|
|
|
|
else
|
|
|
|
|
throw new ArgumentException("conditionOperator", "Must supply a known condition operator");
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 20:25:47 -04:00
|
|
|
|
|
|
|
|
|
|
2018-03-12 19:00:02 -04:00
|
|
|
|
public static IQueryable GroupBy(IQueryable query, Type type, List<(string path, string propertyName)> parts, Type groupToType = null, Type equalityCompareType = null)
|
2018-03-08 22:59:18 -05:00
|
|
|
|
{
|
2018-03-09 00:22:12 -05:00
|
|
|
|
// EXPRESSION
|
2018-03-08 22:59:18 -05:00
|
|
|
|
var parameter = Expression.Parameter(type, "t");
|
2018-03-11 23:16:51 -04:00
|
|
|
|
var partExpressions = new List<(Expression expression, string propertyName)>();
|
2018-03-08 22:59:18 -05:00
|
|
|
|
|
2018-03-09 00:22:12 -05:00
|
|
|
|
var fields = new List<(Type type, string propertyName)>();
|
2018-03-08 22:59:18 -05:00
|
|
|
|
|
2018-03-09 00:22:12 -05:00
|
|
|
|
// resolve part expression and create the fields inside the anonymous type.
|
|
|
|
|
parts.ForEach(part =>
|
2018-03-08 22:59:18 -05:00
|
|
|
|
{
|
2018-03-09 00:22:12 -05:00
|
|
|
|
var partExpression = ResolvePathForExpression(parameter, part.path);
|
|
|
|
|
fields.Add((partExpression.Type, part.propertyName));
|
2018-03-11 23:16:51 -04:00
|
|
|
|
partExpressions.Add((partExpression, part.propertyName));
|
2018-03-08 22:59:18 -05:00
|
|
|
|
});
|
|
|
|
|
|
2018-03-12 22:55:20 -04:00
|
|
|
|
var keyType = groupToType ?? DynamicClassFactory.CreateType(fields);
|
2018-03-11 23:16:51 -04:00
|
|
|
|
|
|
|
|
|
/*
|
2018-03-11 01:58:32 -05:00
|
|
|
|
var constructorTypes = fields.Select(t => t.type).ToArray();
|
|
|
|
|
var constructor = anonymousType.GetConstructor(constructorTypes);
|
|
|
|
|
var newExpression = Expression.New(constructor, partExpressions);
|
|
|
|
|
var genericMethod = Constants.GroupByMethod.MakeGenericMethod(type, anonymousType);
|
2018-03-11 22:54:13 -04:00
|
|
|
|
var lambda = Expression.Lambda(newExpression, parameter);
|
|
|
|
|
var groupByExpression = Expression.Call(genericMethod, query.Expression, lambda);
|
2018-03-11 23:16:51 -04:00
|
|
|
|
var result = query.Provider.CreateQuery(groupByExpression);*/
|
|
|
|
|
|
2018-03-12 19:00:02 -04:00
|
|
|
|
var ctor = Expression.New(keyType);
|
2018-03-13 22:01:21 -04:00
|
|
|
|
var bindings = partExpressions.Select(partExpression => Expression.Bind(keyType.GetProperty(partExpression.propertyName), partExpression.expression)).ToArray();
|
|
|
|
|
var mi = Expression.MemberInit(ctor, bindings);
|
2018-03-11 23:16:51 -04:00
|
|
|
|
var lambda = Expression.Lambda(mi, parameter);
|
2018-03-12 19:00:02 -04:00
|
|
|
|
var genericMethod = equalityCompareType == null ? Constants.GroupByMethod.MakeGenericMethod(type, keyType) : Constants.GroupByMethodWithEqualityComparer.MakeGenericMethod(type, keyType); //, Activator.CreateInstance(equalityCompareType));
|
|
|
|
|
var groupByExpression = equalityCompareType == null ? Expression.Call(genericMethod, query.Expression, lambda) : Expression.Call(genericMethod, query.Expression, lambda, Expression.New(equalityCompareType));
|
2018-03-11 22:54:13 -04:00
|
|
|
|
var result = query.Provider.CreateQuery(groupByExpression);
|
|
|
|
|
return result;
|
2018-03-09 00:22:12 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
public static IQueryable Select(IQueryable query, List<(SelectTypes selectType, string propertyName, string path, SelectCollectionHandling selectCollectionHandling)> parts, Type destinationType = null, bool nullChecking = false)
|
2018-03-13 22:01:21 -04:00
|
|
|
|
{
|
|
|
|
|
// create parameter.
|
|
|
|
|
var queryType = query.ElementType;
|
|
|
|
|
var parameter = Expression.Parameter(queryType, "t");
|
|
|
|
|
|
|
|
|
|
// establish which anynomous types we might need to create.
|
|
|
|
|
var fields = new List<(Type type, string propertyName)>();
|
|
|
|
|
var partExpressions = new List<(Expression expression, string propertyName)>();
|
|
|
|
|
parts.ForEach(part =>
|
|
|
|
|
{
|
2018-03-22 19:16:57 -04:00
|
|
|
|
var partBodyExpression = CreateSelectExpression(query, parameter, part.selectType, part.path, part.selectCollectionHandling, nullChecking);
|
2018-03-13 22:01:21 -04:00
|
|
|
|
fields.Add((partBodyExpression.Type, part.propertyName));
|
|
|
|
|
partExpressions.Add((partBodyExpression, part.propertyName));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// type to use.
|
|
|
|
|
var typeToCreate = destinationType ?? DynamicClassFactory.CreateType(fields);
|
|
|
|
|
var ctor = Expression.New(typeToCreate);
|
|
|
|
|
var bindings = partExpressions.Select(t => Expression.Bind(typeToCreate.GetProperty(t.propertyName), t.expression)).ToArray();
|
|
|
|
|
var mi = Expression.MemberInit(ctor, bindings);
|
|
|
|
|
var lambda = Expression.Lambda(mi, parameter);
|
|
|
|
|
|
|
|
|
|
var selectExpr = Expression.Call(typeof(Queryable), "Select", new[] { query.ElementType, typeToCreate }, query.Expression, lambda);
|
|
|
|
|
var result = query.Provider.CreateQuery(selectExpr);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
private static Expression CreateSelectExpressionForGrouping(IQueryable query, ParameterExpression parameter, SelectTypes selectType, string path, SelectCollectionHandling selectCollectionHandling, bool nullChecking)
|
2018-03-13 22:01:21 -04:00
|
|
|
|
{
|
|
|
|
|
if (selectType == SelectTypes.Key)
|
|
|
|
|
{
|
2018-03-23 20:29:55 -04:00
|
|
|
|
return ResolvePathForExpression(parameter, path);
|
2018-03-13 22:01:21 -04:00
|
|
|
|
}
|
|
|
|
|
else if (selectType == SelectTypes.Count)
|
|
|
|
|
{
|
|
|
|
|
var notGroupedType = parameter.Type.GenericTypeArguments[1];
|
|
|
|
|
var body = Expression.Call(typeof(Enumerable), "Count", new[] { notGroupedType }, parameter);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
else if (selectType == SelectTypes.LongCount)
|
|
|
|
|
{
|
|
|
|
|
var notGroupedType = parameter.Type.GenericTypeArguments[1];
|
|
|
|
|
var body = Expression.Call(typeof(Enumerable), "LongCount", new[] { notGroupedType }, parameter);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
2018-03-13 23:16:58 -04:00
|
|
|
|
else if (selectType == SelectTypes.Average)
|
|
|
|
|
{
|
|
|
|
|
/// https://stackoverflow.com/questions/25482097/call-enumerable-average-via-expression
|
|
|
|
|
var notGroupedType = parameter.Type.GenericTypeArguments[1];
|
|
|
|
|
var innerParameter = Expression.Parameter(notGroupedType);
|
|
|
|
|
var innerMemberExpression = ResolvePathForExpression(innerParameter, path);
|
|
|
|
|
var innerMemberLambda = Expression.Lambda(innerMemberExpression, innerParameter);
|
|
|
|
|
var body = Expression.Call(typeof(Enumerable), "Average", new[] { notGroupedType }, parameter, innerMemberLambda);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
else if (selectType == SelectTypes.Sum)
|
|
|
|
|
{
|
|
|
|
|
var notGroupedType = parameter.Type.GenericTypeArguments[1];
|
|
|
|
|
var innerParameter = Expression.Parameter(notGroupedType);
|
|
|
|
|
var innerMemberExpression = ResolvePathForExpression(innerParameter, path);
|
|
|
|
|
var innerMemberLambda = Expression.Lambda(innerMemberExpression, innerParameter);
|
|
|
|
|
var body = Expression.Call(typeof(Enumerable), "Sum", new[] { notGroupedType }, parameter, innerMemberLambda);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
2018-03-13 23:22:10 -04:00
|
|
|
|
else if (selectType == SelectTypes.ToList)
|
|
|
|
|
{
|
|
|
|
|
var notGroupedType = parameter.Type.GenericTypeArguments[1];
|
|
|
|
|
var body = Expression.Call(typeof(Enumerable), "ToList", new[] { notGroupedType }, parameter);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
2018-03-13 22:01:21 -04:00
|
|
|
|
|
|
|
|
|
throw new NotSupportedException($"unkown select type {selectType}");
|
|
|
|
|
}
|
2018-03-13 23:16:58 -04:00
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
private static Expression CreateSelectExpression(IQueryable query, ParameterExpression parameter, SelectTypes selectType, string path, SelectCollectionHandling selectCollectionHandling, bool nullChecking)
|
2018-03-13 23:16:58 -04:00
|
|
|
|
{
|
|
|
|
|
if (!IsGrouping(query))
|
2018-03-22 19:16:57 -04:00
|
|
|
|
return CreateSelectExpressionRegular(query, parameter, selectType, path, selectCollectionHandling, nullChecking);
|
2018-03-13 23:16:58 -04:00
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
return CreateSelectExpressionForGrouping(query, parameter, selectType, path, selectCollectionHandling, nullChecking);
|
2018-03-13 23:16:58 -04:00
|
|
|
|
}
|
2018-03-21 21:00:46 -04:00
|
|
|
|
|
2018-04-10 21:08:27 -04:00
|
|
|
|
private static Expression CreateSelectExpressionRegular(IQueryable query, ParameterExpression parameter, SelectTypes selectType, string path
|
|
|
|
|
, SelectCollectionHandling selectCollectionHandling, bool nullChecking)
|
2018-03-21 21:00:46 -04:00
|
|
|
|
{
|
|
|
|
|
if (selectType == SelectTypes.Path)
|
|
|
|
|
{
|
2018-03-23 20:29:55 -04:00
|
|
|
|
return ResolvePathForExpression(parameter, path);
|
2018-03-21 21:00:46 -04:00
|
|
|
|
}
|
|
|
|
|
else if (selectType == SelectTypes.PathToList)
|
|
|
|
|
{
|
2018-04-10 21:08:27 -04:00
|
|
|
|
var parser = new ExpressionParser(parameter, path);
|
|
|
|
|
var resolver = new PathExpressionResolver(parser);
|
|
|
|
|
resolver.NullChecking = nullChecking;
|
|
|
|
|
resolver.CollectionHandling = selectCollectionHandling;
|
|
|
|
|
resolver.Resolve();
|
|
|
|
|
var expr = (resolver.Result as LambdaExpression).Body;
|
2018-03-21 21:00:46 -04:00
|
|
|
|
var notGroupedType = expr.Type.GenericTypeArguments.FirstOrDefault();
|
|
|
|
|
if (notGroupedType == null)
|
|
|
|
|
throw new Exception($"Path must be a Enumerable<T> but its a {expr.Type}");
|
2018-03-23 00:34:46 -04:00
|
|
|
|
|
|
|
|
|
var body = Expression.Call(typeof(Enumerable), "ToList", new[] { notGroupedType }, expr) as Expression;
|
2018-03-21 21:00:46 -04:00
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NotSupportedException($"unkown select type {selectType}");
|
|
|
|
|
}
|
2018-03-13 23:16:58 -04:00
|
|
|
|
|
|
|
|
|
private static bool IsGrouping(IQueryable query)
|
|
|
|
|
{
|
|
|
|
|
// TODO needs to be alot better than this, but it will do for now.
|
|
|
|
|
if (query.ElementType.Name.Contains("IGrouping"))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-03-13 22:01:21 -04:00
|
|
|
|
|
2018-03-09 00:22:12 -05:00
|
|
|
|
public static IQueryable GroupBy(IQueryable query, Type type, string path)
|
|
|
|
|
{
|
|
|
|
|
var parameter = Expression.Parameter(type, "t");
|
|
|
|
|
var field = QueryableHelpers.ResolvePathForExpression(parameter, path);
|
|
|
|
|
var lambda = Expression.Lambda(field, parameter);
|
|
|
|
|
var genericMethod = Constants.GroupByMethod.MakeGenericMethod(type, field.Type);
|
|
|
|
|
var groupByEpression = Expression.Call(genericMethod, query.Expression, lambda);
|
|
|
|
|
var result = query.Provider.CreateQuery(groupByEpression);
|
|
|
|
|
return result;
|
2018-03-08 22:59:18 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 20:02:50 -04:00
|
|
|
|
internal static Expression InternalResolvePathExpression(int step, Expression param, List<string> parts, SelectCollectionHandling selectCollectionHandling, bool nullChecking, Type nullCheckingType = null)
|
2018-03-21 23:15:45 -04:00
|
|
|
|
{
|
|
|
|
|
var isLast = parts.Count == 1;
|
|
|
|
|
var currentPart = parts.First();
|
|
|
|
|
var memberExpression = Expression.PropertyOrField(param, currentPart);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isLast)
|
|
|
|
|
return memberExpression;
|
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
Expression ret = null;
|
|
|
|
|
|
2018-03-23 20:29:55 -04:00
|
|
|
|
if (!IsGenericEnumerable(memberExpression))
|
2018-03-22 19:16:57 -04:00
|
|
|
|
{
|
|
|
|
|
// TODO: null checking here too.
|
|
|
|
|
// should be easier then collection :=|
|
2018-03-21 23:15:45 -04:00
|
|
|
|
|
2018-03-23 20:02:50 -04:00
|
|
|
|
ret = InternalResolvePathExpression(step + 1, memberExpression, parts.Skip(1).ToList(), selectCollectionHandling, nullChecking, nullCheckingType: nullCheckingType);
|
2018-03-22 19:16:57 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// enumerable.
|
|
|
|
|
var listGenericArgumentType = memberExpression.Type.GetGenericArguments().First();
|
2018-03-21 23:15:45 -04:00
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
// sub param.
|
|
|
|
|
var innerParam = Expression.Parameter(listGenericArgumentType);
|
|
|
|
|
var innerExpression = InternalResolvePathExpression(step + 1, innerParam, parts.Skip(1).ToList(), selectCollectionHandling, nullChecking);
|
|
|
|
|
var lambda = Expression.Lambda(innerExpression, innerParam);
|
2018-03-21 23:15:45 -04:00
|
|
|
|
|
2018-03-26 17:14:01 -04:00
|
|
|
|
if (selectCollectionHandling == SelectCollectionHandling.LeaveAsIs)
|
2018-03-22 19:16:57 -04:00
|
|
|
|
ret = Expression.Call(typeof(Enumerable), "Select", new Type[] { listGenericArgumentType, innerExpression.Type }, memberExpression, lambda);
|
|
|
|
|
else
|
|
|
|
|
ret = Expression.Call(typeof(Enumerable), "SelectMany", new Type[] { listGenericArgumentType, innerExpression.Type.GenericTypeArguments.First() }, memberExpression, lambda);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 20:29:55 -04:00
|
|
|
|
if (step == 1 && nullChecking)
|
|
|
|
|
{
|
|
|
|
|
ret = Expression.Condition(
|
|
|
|
|
Expression.Equal(memberExpression, Expression.Constant(null)),
|
|
|
|
|
Expression.New(nullCheckingType),
|
|
|
|
|
ret
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 19:16:57 -04:00
|
|
|
|
return ret;
|
2018-03-21 23:15:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 20:55:29 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the right expression for a path supplied.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="param">Expression.Parameter(typeOfClassOrInterface)</param>
|
|
|
|
|
/// <param name="path">the path you wish to resolve example Contact.Profile.FirstName</param>
|
|
|
|
|
/// <returns></returns>
|
2018-04-11 23:18:10 -04:00
|
|
|
|
public static Expression ResolvePathForExpression(ParameterExpression param, string path, bool throwIfHasEnumerable = true)
|
2018-02-11 20:55:29 -05:00
|
|
|
|
{
|
2018-04-11 23:18:10 -04:00
|
|
|
|
var expressionParser = new ExpressionParser(param, path);
|
|
|
|
|
expressionParser.Parse();
|
2018-03-26 21:42:49 -04:00
|
|
|
|
|
2018-04-11 23:18:10 -04:00
|
|
|
|
if (throwIfHasEnumerable && expressionParser.Pieces.Any(t2 => t2.IsGenericEnumerable))
|
|
|
|
|
throw new Exception("Path contains an enumerable, and this feature does not support it.");
|
|
|
|
|
|
|
|
|
|
var expressionResolver = new PathExpressionResolver(expressionParser);
|
|
|
|
|
expressionResolver.Resolve();
|
|
|
|
|
return expressionResolver.GetResultBodyExpression();
|
2018-02-11 20:55:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ConstantExpression GetConstantSameAsLeftOperator(Expression member, object value)
|
|
|
|
|
{
|
|
|
|
|
if (member == null)
|
|
|
|
|
throw new ArgumentNullException("member");
|
|
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
return Expression.Constant(null);
|
|
|
|
|
|
|
|
|
|
// the types.
|
|
|
|
|
var valueType = value.GetType();
|
|
|
|
|
var memberType = member.Type;
|
|
|
|
|
|
|
|
|
|
// if match.
|
|
|
|
|
if (valueType == memberType)
|
|
|
|
|
return Expression.Constant(value);
|
|
|
|
|
|
|
|
|
|
// attempt a conversion.
|
|
|
|
|
object convertedValue = TypeHelpers.ConvertFrom(memberType, value);
|
2018-03-06 19:34:28 -05:00
|
|
|
|
return Expression.Constant(convertedValue, memberType);
|
2018-02-11 20:55:29 -05:00
|
|
|
|
}
|
2018-02-12 04:30:55 -05:00
|
|
|
|
|
|
|
|
|
public static ConstantExpression ResolveConstant(Expression member, object value, QueryConvertStrategy convertStrategy)
|
|
|
|
|
{
|
|
|
|
|
if (convertStrategy == QueryConvertStrategy.LeaveAsIs)
|
|
|
|
|
return Expression.Constant(value);
|
|
|
|
|
|
2018-03-06 19:34:28 -05:00
|
|
|
|
if (convertStrategy == QueryConvertStrategy.SpecifyType)
|
|
|
|
|
return Expression.Constant(value, member.Type);
|
|
|
|
|
|
2018-02-12 04:30:55 -05:00
|
|
|
|
if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
|
|
|
|
|
return QueryableHelpers.GetConstantSameAsLeftOperator(member, value);
|
|
|
|
|
|
|
|
|
|
throw new NotSupportedException($"{convertStrategy} supplied is not recognized");
|
|
|
|
|
}
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
2018-03-14 20:25:47 -04:00
|
|
|
|
public static IQueryable CreateOrderByExpression(IQueryable query, string path, QueryOrderByDirection direction, bool append = true)
|
2018-02-12 05:18:44 -05:00
|
|
|
|
{
|
2018-03-14 19:50:43 -04:00
|
|
|
|
var parameter = Expression.Parameter(query.ElementType, "t");
|
2018-03-14 20:25:47 -04:00
|
|
|
|
var member = QueryableHelpers.ResolvePathForExpression(parameter, path);
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
|
|
|
|
string sortCommand = null;
|
2018-03-14 20:25:47 -04:00
|
|
|
|
if (direction == QueryOrderByDirection.Descending)
|
|
|
|
|
sortCommand = append == false ? "OrderByDescending" : "ThenByDescending";
|
2018-02-12 05:18:44 -05:00
|
|
|
|
else
|
2018-03-14 20:25:47 -04:00
|
|
|
|
sortCommand = append == false ? "OrderBy" : "ThenBy";
|
2018-02-12 05:18:44 -05:00
|
|
|
|
|
|
|
|
|
var expression = Expression.Lambda(member, parameter);
|
|
|
|
|
|
|
|
|
|
var resultExpression = Expression.Call
|
|
|
|
|
(typeof(Queryable),
|
|
|
|
|
sortCommand,
|
2018-03-14 19:50:43 -04:00
|
|
|
|
new Type[] { query.ElementType, member.Type },
|
2018-02-12 05:18:44 -05:00
|
|
|
|
query.Expression,
|
|
|
|
|
Expression.Quote(expression)
|
|
|
|
|
);
|
|
|
|
|
|
2018-03-14 19:50:43 -04:00
|
|
|
|
query = query.Provider.CreateQuery(resultExpression);
|
2018-02-12 05:18:44 -05:00
|
|
|
|
return query;
|
|
|
|
|
}
|
2018-02-12 21:28:53 -05:00
|
|
|
|
|
2018-03-06 22:04:54 -05:00
|
|
|
|
/*
|
|
|
|
|
* var methodInfo = typeof(List<Guid>).GetMethod("Contains", new Type[] { typeof(Guid) });
|
|
|
|
|
var list = Expression.Constant(ids);
|
|
|
|
|
var param = Expression.Parameter(typeof(T), "t");
|
|
|
|
|
var value = Expression.PropertyOrField(param, idField);
|
|
|
|
|
var body = Expression.Call(list, methodInfo, value);
|
|
|
|
|
var lambda = Expression.Lambda<Func<T, bool>>(body, param);
|
|
|
|
|
query = query.Where(lambda);
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-14 19:26:30 -04:00
|
|
|
|
internal static Expression InternalCreateConditionExpression(int recursionStep, Type type, ParameterExpression parameter, Expression current, List<string> parts,
|
2018-03-06 20:43:49 -05:00
|
|
|
|
ConditionOperators condition, object value, QueryConvertStrategy convertStrategy, QueryCollectionHandling collectionHandling, bool nullChecking, StringComparison? stringComparison)
|
2018-02-12 21:28:53 -05:00
|
|
|
|
{
|
|
|
|
|
var partStr = parts.First();
|
|
|
|
|
var isLast = parts.Count == 1;
|
|
|
|
|
|
|
|
|
|
// the member expression.
|
|
|
|
|
var memberExpression = Expression.PropertyOrField(current, partStr);
|
|
|
|
|
|
|
|
|
|
// TODO : maybe support that last part is collection but what do we do?
|
|
|
|
|
// not supported yet.
|
2018-03-23 20:29:55 -04:00
|
|
|
|
if (isLast && IsGenericEnumerable(memberExpression) && value != null)
|
2018-02-12 22:27:09 -05:00
|
|
|
|
throw new NotSupportedException("Can only compare collection to null");
|
|
|
|
|
|
2018-02-12 21:28:53 -05:00
|
|
|
|
|
|
|
|
|
// create the expression and return it.
|
|
|
|
|
if (isLast)
|
|
|
|
|
{
|
2018-03-06 22:04:54 -05:00
|
|
|
|
if (condition == ConditionOperators.In || condition == ConditionOperators.NotIn)
|
|
|
|
|
{
|
|
|
|
|
return InAndNotIn(parameter, condition, value, convertStrategy, memberExpression);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var constant = QueryableHelpers.ResolveConstant(memberExpression, value, convertStrategy);
|
|
|
|
|
var filterExpression = QueryableHelpers.GetConditionExpressionForMember(parameter, memberExpression, condition, constant, stringComparison);
|
|
|
|
|
var lambda = Expression.Lambda(filterExpression, parameter);
|
|
|
|
|
return lambda;
|
|
|
|
|
}
|
2018-02-12 21:28:53 -05:00
|
|
|
|
}
|
2018-02-12 22:27:09 -05:00
|
|
|
|
|
|
|
|
|
// null check.
|
|
|
|
|
Expression nullCheckExpression = null;
|
|
|
|
|
if (nullChecking)
|
|
|
|
|
nullCheckExpression = Expression.NotEqual(memberExpression, Expression.Constant(null));
|
|
|
|
|
|
2018-03-23 20:29:55 -04:00
|
|
|
|
if (IsGenericEnumerable(memberExpression))
|
2018-02-12 21:28:53 -05:00
|
|
|
|
{
|
|
|
|
|
var listGenericArgumentType = memberExpression.Type.GetGenericArguments().First();
|
|
|
|
|
var innerParameter = Expression.Parameter(listGenericArgumentType, $"t{++recursionStep}");
|
2018-03-14 19:26:30 -04:00
|
|
|
|
var innerLambda = InternalCreateConditionExpression(recursionStep, listGenericArgumentType, innerParameter, innerParameter, parts.Skip(1).ToList(), condition, value, convertStrategy, collectionHandling, nullChecking, stringComparison);
|
2018-02-12 21:28:53 -05:00
|
|
|
|
|
|
|
|
|
// the collection method.
|
|
|
|
|
var collectionMethod = GetCollectionMethod(collectionHandling);
|
|
|
|
|
var genericMethod = collectionMethod.MakeGenericMethod(listGenericArgumentType);
|
|
|
|
|
var callResult = Expression.Call(genericMethod, memberExpression, innerLambda);
|
|
|
|
|
|
2018-02-12 22:27:09 -05:00
|
|
|
|
if (nullCheckExpression != null)
|
|
|
|
|
{
|
|
|
|
|
var nullCheckResult = Expression.AndAlso(nullCheckExpression, callResult);
|
|
|
|
|
return Expression.Lambda(nullCheckResult, parameter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Expression.Lambda(callResult, parameter);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (nullCheckExpression != null)
|
|
|
|
|
{
|
2018-03-14 19:26:30 -04:00
|
|
|
|
var pathExpr = InternalCreateConditionExpression(recursionStep, type, parameter, memberExpression, parts.Skip(1).ToList(), condition, value, convertStrategy, collectionHandling, nullChecking, stringComparison);
|
2018-04-04 22:57:40 -04:00
|
|
|
|
|
|
|
|
|
var nullCheckResult = Expression.AndAlso(nullCheckExpression, (pathExpr as LambdaExpression).Body);
|
|
|
|
|
var nullCheckResultLambda = Expression.Lambda((Expression)nullCheckResult, parameter);
|
|
|
|
|
return nullCheckResultLambda;
|
2018-02-12 22:27:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 19:26:30 -04:00
|
|
|
|
return InternalCreateConditionExpression(recursionStep, type, parameter, memberExpression, parts.Skip(1).ToList(), condition, value, convertStrategy, collectionHandling, nullChecking, stringComparison);
|
2018-02-12 22:27:09 -05:00
|
|
|
|
}
|
2018-02-12 21:28:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 22:04:54 -05:00
|
|
|
|
public static Expression InAndNotIn(ParameterExpression parameter, ConditionOperators condition, object value, QueryConvertStrategy convertStrategy, MemberExpression memberExpression)
|
|
|
|
|
{
|
|
|
|
|
var enumerableValue = value as IEnumerable;
|
|
|
|
|
if (enumerableValue == null)
|
|
|
|
|
throw new Exception($"to use {ConditionOperators.In} your value must at least be IEnumerable");
|
|
|
|
|
|
|
|
|
|
var enumerableType = GetEnumerableType(enumerableValue);
|
|
|
|
|
var finalType = convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField ? memberExpression.Type : enumerableType;
|
|
|
|
|
var genericListOfEnumerableType = typeof(List<>).MakeGenericType(memberExpression.Type);
|
|
|
|
|
var containsMethod = genericListOfEnumerableType.GetMethod("Contains", new Type[] { finalType });
|
|
|
|
|
var list = Activator.CreateInstance(genericListOfEnumerableType) as IList;
|
|
|
|
|
foreach (var o in enumerableValue)
|
|
|
|
|
{
|
|
|
|
|
if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
|
|
|
|
|
list.Add(TypeHelpers.ConvertFrom(memberExpression.Type, o));
|
|
|
|
|
else
|
|
|
|
|
list.Add(o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var body = Expression.Call(Expression.Constant(list), containsMethod, memberExpression) as Expression;
|
|
|
|
|
|
|
|
|
|
if (condition == ConditionOperators.NotIn)
|
|
|
|
|
body = Expression.Not(body);
|
|
|
|
|
|
|
|
|
|
var lambda = Expression.Lambda(body, parameter);
|
|
|
|
|
return lambda;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Type GetEnumerableType(IEnumerable enumerableValue)
|
|
|
|
|
{
|
|
|
|
|
foreach (var o in enumerableValue)
|
|
|
|
|
return o.GetType();
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 22:27:09 -05:00
|
|
|
|
public static MethodInfo GetCollectionMethod(QueryCollectionHandling collectionHandling)
|
2018-02-12 21:28:53 -05:00
|
|
|
|
{
|
2018-02-12 22:27:09 -05:00
|
|
|
|
if (collectionHandling == QueryCollectionHandling.All)
|
2018-02-12 21:28:53 -05:00
|
|
|
|
return Constants.AllMethod;
|
2018-02-12 22:27:09 -05:00
|
|
|
|
else if (collectionHandling == QueryCollectionHandling.Any)
|
2018-02-12 21:28:53 -05:00
|
|
|
|
return Constants.AnyMethod;
|
|
|
|
|
|
|
|
|
|
throw new NotSupportedException($"{collectionHandling} is not supported");
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 22:01:21 -04:00
|
|
|
|
|
2018-03-14 20:25:47 -04:00
|
|
|
|
public static Expression<Func<T, bool>> CreateConditionExpression<T>(
|
|
|
|
|
string path,
|
|
|
|
|
ConditionOperators condition,
|
|
|
|
|
object value,
|
|
|
|
|
QueryConvertStrategy convertStrategy,
|
|
|
|
|
QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any,
|
|
|
|
|
ParameterExpression parameter = null,
|
|
|
|
|
bool nullChecking = false,
|
|
|
|
|
StringComparison? stringComparision = null)
|
|
|
|
|
{
|
|
|
|
|
var ret = CreateConditionExpression(typeof(T), path, condition, value, convertStrategy,
|
|
|
|
|
collectionHandling: collectionHandling,
|
|
|
|
|
parameter: parameter,
|
|
|
|
|
nullChecking: nullChecking,
|
|
|
|
|
stringComparision: stringComparision) as Expression<Func<T, bool>>;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Expression CreateConditionExpression(Type type,
|
|
|
|
|
string path,
|
2018-02-12 21:28:53 -05:00
|
|
|
|
ConditionOperators condition,
|
|
|
|
|
object value,
|
|
|
|
|
QueryConvertStrategy convertStrategy,
|
2018-02-12 22:27:09 -05:00
|
|
|
|
QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any,
|
|
|
|
|
ParameterExpression parameter = null,
|
2018-03-06 20:43:49 -05:00
|
|
|
|
bool nullChecking = false,
|
|
|
|
|
StringComparison? stringComparision = null)
|
2018-02-12 21:28:53 -05:00
|
|
|
|
{
|
|
|
|
|
if (parameter == null)
|
2018-03-14 20:25:47 -04:00
|
|
|
|
parameter = Expression.Parameter(type, "t");
|
2018-02-12 21:28:53 -05:00
|
|
|
|
|
|
|
|
|
var parts = path.Split('.').ToList();
|
2018-03-14 20:25:47 -04:00
|
|
|
|
var result = InternalCreateConditionExpression(1, type, parameter, parameter, parts, condition, value, convertStrategy, collectionHandling, nullChecking, stringComparision);
|
|
|
|
|
return result;
|
2018-02-12 21:28:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-26 17:14:01 -04:00
|
|
|
|
public static bool IsGenericEnumerable(Expression member) => IsGenericEnumerable(member.Type);
|
2018-03-23 20:29:55 -04:00
|
|
|
|
public static bool IsGenericEnumerable(Type type)
|
|
|
|
|
{
|
|
|
|
|
if (!type.IsGenericType)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var genericArgumentType = type.GenericTypeArguments.First();
|
|
|
|
|
var ret = typeof(IEnumerable<>).MakeGenericType(genericArgumentType).IsAssignableFrom(type);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-02-11 20:55:29 -05:00
|
|
|
|
}
|
|
|
|
|
}
|