Reputation: 65
I want to dynamically build LinqToSQL and EntityFramework query clauses using a "criteria" object and if necessary leveraging the C# In a Nutshell PredicateBuilder (http://www.albahari.com/nutshell/predicatebuilder.aspx)
Just to be clear - I don't understand how to create C# expressions ;o)
The filter operator and value are defined as a property of my "query criteria" class as such:
public class QueryCriteria
{
public DateTimeFilter DateTimeCompleted { get; set; }
}
The "filter" defined as:
public class DateTimeFilter
{
public FilterOperator FilterOperator { get; set; }
public DateTime FilterToken { get; set; }
}
And the "filter operator" is a plain C# enum:
public enum FilterOperator : int
{
ExactMatch = 1,
BeginsWith = 2,
Contains = 3,
EndsWith = 4,
GreaterThan = 5,
GreaterThanOrEqualTo = 6,
LessThan = 7,
LessThanOrEqualTo = 8,
}
I want to dynamically build my LinqToSQL and EntityFramework queries in my repositories using an approach such as:
if (criteria.DateTimeCompleted != null)
{
var predicate = PredicateBuilder.True(query);
switch (criteria.DateTimeCompleted.FilterOperator)
{
case FilterOperator.ExactMatch:
predicate = predicate.And(p => string.Equals(p.DateTimeCompleted.Value, criteria.DateTimeCompleted.FilterToken));
break;
case FilterOperator.GreaterThan:
predicate = predicate.And(p => p.DateTimeCompleted.Value > criteria.DateTimeCompleted.FilterToken);
break;
case FilterOperator.GreaterThanOrEqualTo:
predicate = predicate.And(p => p.DateTimeCompleted.Value >= criteria.DateTimeCompleted.FilterToken);
break;
case FilterOperator.LessThan:
predicate = predicate.And(p => p.DateTimeCompleted.Value < criteria.DateTimeCompleted.FilterToken);
break;
case FilterOperator.LessThanOrEqualTo:
predicate = predicate.And(p => p.DateTimeCompleted.Value <= criteria.DateTimeCompleted.FilterToken);
break;
default:
throw new InvalidOperationException("Can't use the specified filter operator on a DateTime.");
}
query = query.Where(predicate);
}
The above all works but I want my code to be DRY and when I add another property to my criteria (e.g. DateTimeStarted) I don't want my repository to duplicate the code immediately above for the new property.
So I want to simplify the above code to something like:
query = query.Where(criteria.DateTimeCompleted.GetPredicate(query, p => p.DateTimeCompleted.Value));
Is this possible and if so how?
Upvotes: 3
Views: 1733
Reputation: 205839
It's definitely possible (and there is no need for PredicateBuilder). All you need is to map the supported FilterOperator
to the corresponding Expression
.
Here is a custom extension method that does that:
public static class QueryableExtensions
{
public static IQueryable<T> Where<T>(this IQueryable<T> source, DateTimeFilter filter, Expression<Func<T, DateTime>> target)
{
if (filter == null) return source;
var left = target.Body;
var right = Expression.Constant(filter.FilterToken);
Expression condition;
switch (filter.FilterOperator)
{
case FilterOperator.ExactMatch:
condition = Expression.Equal(left, right);
break;
case FilterOperator.GreaterThan:
condition = Expression.GreaterThan(left, right);
break;
case FilterOperator.GreaterThanOrEqualTo:
condition = Expression.GreaterThanOrEqual(left, right);
break;
case FilterOperator.LessThan:
condition = Expression.LessThan(left, right);
break;
case FilterOperator.LessThanOrEqualTo:
condition = Expression.LessThanOrEqual(left, right);
break;
default:
throw new InvalidOperationException("Can't use the specified filter operator on a DateTime.");
}
var predicate = Expression.Lambda<Func<T, bool>>(condition, target.Parameters);
return source.Where(predicate);
}
}
and the sample usage:
query = query.Where(criteria.DateTimeCompleted, p => p.DateTimeCompleted.Value);
Upvotes: 5