So, this is rather complicated.
I have a set of rules in a collection, a rule contains these three properties.
Field, Op, and Data (all strings)
So a rule might look like "State", "eq", "CA"
My general rules are that all rules are ANDed together. However, with the caveat that, if they have the same Field value, those all ORed. This allows us to say "State", "eq", "CA", OR "State", "eq", "TX" AND "FirstName", "eq", "John".
The issue is that my current way of applying rules won't work, because it just keeps building up the linq expression using each rule to make it more and more explicit.
var result = rules.Aggregate(_repository.All, (current, rule) => current.ExtendQuery(rule))
ExtendQuery is an extension method I wrote, it uses ExpressionTrees, to generate a new query, that applies the current rule to the passed in query. (effectively ANDing them all together)
Now it wouldn't be hard for me to modify the .Aggregate line to group the rules by Field, and then generate the a unique query for each Field, but then how do I get it to "OR" them together instead of "AND"?
And then with each of those queries, how would I "AND" them together? A Union?
ExtendQuery looks like this
public static IQueryable<T> ExtendQuery<T>(this IQueryable<T> query, QueryableRequestMessage.WhereClause.Rule rule) where T : class
{
var parameter = Expression.Parameter(typeof(T), "x");
Expression property = Expression.Property(parameter, rule.Field);
var type = property.Type;
ConstantExpression constant;
if (type.IsEnum)
{
var enumeration = Enum.Parse(type, rule.Data);
var intValue = (int)enumeration;
constant = Expression.Constant(intValue);
type = typeof(int);
//Add "Id" by convention, this is all because enum support is lacking at this point in Entity Framework
property = Expression.Property(parameter, rule.Field + "Id");
}
else if(type == typeof(DateTime))
{
constant = Expression.Constant(DateTime.ParseExact(rule.Data, "dd/MM/yyyy", CultureInfo.CurrentCulture));
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
//This will convert rule.Data to the baseType, not a nullable type (because that won't work)
var converter = TypeDescriptor.GetConverter(type);
var value = converter.ConvertFrom(rule.Data);
constant = Expression.Constant(value);
//We change the type of property to get converted to it's base type
//This is because Expression.GreaterThanOrEqual can't compare a decimal with a Nullable<decimal>
var baseType = type.GetTypeOfNullable();
property = Expression.Convert(property, baseType);
}
else
{
constant = Expression.Constant(Convert.ChangeType(rule.Data, type));
}
switch (rule.Op)
{
case "eq": //Equals
case "ne": //NotEquals
{
var condition = rule.Op.Equals("eq")
? Expression.Equal(property, constant)
: Expression.NotEqual(property, constant);
var lambda = Expression.Lambda(condition, parameter);
var call = Expression.Call(typeof(Queryable), "Where", new[] { query.ElementType }, query.Expression, lambda);
query = query.Provider.CreateQuery<T>(call);
break;
}
case "lt": //Less Than
query = type == typeof (String)
? QueryExpressionString(query, Expression.LessThan, type, property, constant, parameter)
: QueryExpression(query, Expression.LessThan, property, constant, parameter); break;
case "le": //Less Than or Equal To
query = type == typeof (String)
? QueryExpressionString(query, Expression.LessThanOrEqual, type, property, constant, parameter)
: QueryExpression(query, Expression.LessThanOrEqual, property, constant, parameter); break;
case "gt": //Greater Than
query = type == typeof (String)
? QueryExpressionString(query, Expression.GreaterThan, type, property, constant, parameter)
: QueryExpression(query, Expression.GreaterThan, property, constant, parameter); break;
case "ge": //Greater Than or Equal To
query = type == typeof (String)
? QueryExpressionString(query, Expression.GreaterThanOrEqual, type, property, constant, parameter)
: QueryExpression(query, Expression.GreaterThanOrEqual, property, constant, parameter); break;
case "bw": //Begins With
case "bn": //Does Not Begin With
query = QueryMethod(query, rule, type, "StartsWith", property, constant, "bw", parameter); break;
case "ew": //Ends With
case "en": //Does Not End With
query = QueryMethod(query, rule, type, "EndsWith", property, constant, "cn", parameter); break;
case "cn": //Contains
case "nc": //Does Not Contain
query = QueryMethod(query, rule, type, "Contains", property, constant, "cn", parameter); break;
case "nu": //TODO: Null
case "nn": //TODO: Not Null
break;
}
return query;
}
private static IQueryable<T> QueryExpression<T>(
IQueryable<T> query,
Func<Expression, Expression, BinaryExpression> expression,
Expression property,
Expression value,
ParameterExpression parameter
) where T : class
{
var condition = expression(property, value);
var lambda = Expression.Lambda(condition, parameter);
var call = Expression.Call(typeof(Queryable), "Where", new[] { query.ElementType }, query.Expression, lambda);
query = query.Provider.CreateQuery<T>(call);
return query;
}
private static IQueryable<T> QueryExpressionString<T>(
IQueryable<T> query,
Func<Expression, Expression, BinaryExpression> expression,
Type type,
Expression property,
Expression value,
ParameterExpression parameter)
{
var containsmethod = type.GetMethod("CompareTo", new[] { type });
var callContains = Expression.Call(property, containsmethod, value);
var call = expression(callContains, Expression.Constant(0, typeof(int)));
return query.Where(Expression.Lambda<Func<T, bool>>(call, parameter));
}
private static IQueryable<T> QueryMethod<T>(
IQueryable<T> query,
QueryableRequestMessage.WhereClause.Rule rule,
Type type,
string methodName,
Expression property,
Expression value,
string op,
ParameterExpression parameter
) where T : class
{
var containsmethod = type.GetMethod(methodName, new[] { type });
var call = Expression.Call(property, containsmethod, value);
var expression = rule.Op.Equals(op)
? Expression.Lambda<Func<T, bool>>(call, parameter)
: Expression.Lambda<Func<T, bool>>(Expression.IsFalse(call), parameter);
query = query.Where(expression);
return query;
}