added query convert strategy.

This commit is contained in:
David 2018-02-12 03:30:55 -06:00
parent 8e88e13861
commit b1db7a3604
5 changed files with 47 additions and 17 deletions

View File

@ -19,6 +19,12 @@ namespace PoweredSoft.DynamicLinq
EndsWith EndsWith
} }
public enum QueryConvertStrategy
{
LeaveAsIs,
ConvertConstantToComparedPropertyOrField
}
internal static class Constants internal static class Constants
{ {
internal static readonly MethodInfo ContainsMethod = typeof(string).GetMethod("Contains"); internal static readonly MethodInfo ContainsMethod = typeof(string).GetMethod("Contains");

View File

@ -11,9 +11,10 @@ namespace PoweredSoft.DynamicLinq.Extensions
{ {
public static class QueryableExtensions public static class QueryableExtensions
{ {
public static IQueryable<T> Where<T>(this IQueryable<T> query, string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true) public static IQueryable<T> Where<T>(this IQueryable<T> query, string path, ConditionOperators conditionOperator, object value,
QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
{ {
query = query.Query(qb => qb.Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator)); query = query.Query(qb => qb.Compare(path, conditionOperator, value, convertStrategy: convertStrategy));
return query; return query;
} }

View File

@ -21,8 +21,9 @@ namespace PoweredSoft.DynamicLinq.Fluent
Query = query; Query = query;
} }
public QueryBuilder<T> Compare(string path, ConditionOperators conditionOperators, object value, public virtual QueryBuilder<T> Compare(string path, ConditionOperators conditionOperators, object value,
bool convertConstantToLeftOperator = true, bool and = true) QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
bool and = true)
{ {
Parts.Add(new QueryFilterPart Parts.Add(new QueryFilterPart
{ {
@ -30,13 +31,13 @@ namespace PoweredSoft.DynamicLinq.Fluent
ConditionOperator = conditionOperators, ConditionOperator = conditionOperators,
Path = path, Path = path,
Value = value, Value = value,
ConvertConstantToLeftOperator = convertConstantToLeftOperator ConvertStrategy = convertStrategy
}); });
return this; return this;
} }
public QueryBuilder<T> SubQuery(Action<QueryBuilder<T>> subQuery, bool and = true) public virtual QueryBuilder<T> SubQuery(Action<QueryBuilder<T>> subQuery, bool and = true)
{ {
// create query builder for same type. // create query builder for same type.
var qb = new QueryBuilder<T>(Query); var qb = new QueryBuilder<T>(Query);
@ -54,11 +55,11 @@ namespace PoweredSoft.DynamicLinq.Fluent
return this; return this;
} }
public QueryBuilder<T> And(string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true) public QueryBuilder<T> And(string path, ConditionOperators conditionOperator, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
=> Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator, and: true); => Compare(path, conditionOperator, value, convertStrategy: convertStrategy, and: true);
public QueryBuilder<T> Or(string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true) public QueryBuilder<T> Or(string path, ConditionOperators conditionOperator, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
=> Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator, and: false); => Compare(path, conditionOperator, value, convertStrategy: convertStrategy, and: false);
public QueryBuilder<T> And(Action<QueryBuilder<T>> subQuery) public QueryBuilder<T> And(Action<QueryBuilder<T>> subQuery)
=> SubQuery(subQuery, true); => SubQuery(subQuery, true);
@ -66,16 +67,28 @@ namespace PoweredSoft.DynamicLinq.Fluent
public QueryBuilder<T> Or(Action<QueryBuilder<T>> subQuery) public QueryBuilder<T> Or(Action<QueryBuilder<T>> subQuery)
=> SubQuery(subQuery, false); => SubQuery(subQuery, false);
public IQueryable<T> Build() public virtual IQueryable<T> Build()
{
// the query.
var query = Query;
// execute the filters.
query = ExecuteFilters(query);
return query;
}
protected virtual IQueryable<T> ExecuteFilters(IQueryable<T> query)
{ {
var parameter = Expression.Parameter(typeof(T), "t"); var parameter = Expression.Parameter(typeof(T), "t");
var expression = BuildExpression(parameter, Parts); var expression = BuildExpression(parameter, Parts);
var lambda = Expression.Lambda<Func<T, bool>>(expression, parameter); var lambda = Expression.Lambda<Func<T, bool>>(expression, parameter);
var query = Query.Where(lambda); query = query.Where(lambda);
return query; return query;
} }
protected Expression BuildExpression(ParameterExpression parameter, List<QueryFilterPart> parts) protected virtual Expression BuildExpression(ParameterExpression parameter, List<QueryFilterPart> parts)
{ {
Expression ret = null; Expression ret = null;
@ -96,10 +109,10 @@ namespace PoweredSoft.DynamicLinq.Fluent
return ret; return ret;
} }
private Expression BuildExpression(ParameterExpression parameter, QueryFilterPart part) protected virtual Expression BuildExpression(ParameterExpression parameter, QueryFilterPart part)
{ {
var member = QueryableHelpers.ResolvePathForExpression(parameter, part.Path); var member = QueryableHelpers.ResolvePathForExpression(parameter, part.Path);
var constant = part.ConvertConstantToLeftOperator ? QueryableHelpers.GetConstantSameAsLeftOperator(member, part.Value) : Expression.Constant(part.Value); var constant = QueryableHelpers.ResolveConstant(member, part.Value, part.ConvertStrategy);
var expression = QueryableHelpers.GetConditionExpressionForMember(parameter, member, part.ConditionOperator, constant); var expression = QueryableHelpers.GetConditionExpressionForMember(parameter, member, part.ConditionOperator, constant);
return expression; return expression;
} }

View File

@ -12,8 +12,7 @@ namespace PoweredSoft.DynamicLinq.Fluent
public ConditionOperators ConditionOperator { get; set; } public ConditionOperators ConditionOperator { get; set; }
public object Value { get; set; } public object Value { get; set; }
public bool And { get; set; } public bool And { get; set; }
public bool ConvertConstantToLeftOperator { get; set; } public QueryConvertStrategy ConvertStrategy { get; set; }
public List<QueryFilterPart> Parts { get; set; } = new List<QueryFilterPart>(); public List<QueryFilterPart> Parts { get; set; } = new List<QueryFilterPart>();
} }
} }

View File

@ -80,5 +80,16 @@ namespace PoweredSoft.DynamicLinq.Helpers
object convertedValue = TypeHelpers.ConvertFrom(memberType, value); object convertedValue = TypeHelpers.ConvertFrom(memberType, value);
return Expression.Constant(convertedValue); return Expression.Constant(convertedValue);
} }
public static ConstantExpression ResolveConstant(Expression member, object value, QueryConvertStrategy convertStrategy)
{
if (convertStrategy == QueryConvertStrategy.LeaveAsIs)
return Expression.Constant(value);
if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
return QueryableHelpers.GetConstantSameAsLeftOperator(member, value);
throw new NotSupportedException($"{convertStrategy} supplied is not recognized");
}
} }
} }