PredicateBuilder reusable DateTime predicate setter - c#

I'm using PredicateBuilder to generate dynamic search clauses. In the sample code below, is there a way that I can modify SetDateTimePredicate so it can be used for any DateTime property on SomeType?
Expression<Func<SomeType, bool>> somePredicate = null;
somePredicate = somePredicate.Or(
SetDateTimePredicate(comparisonOperator, dateTime1, dateTime2));
private Expression<Func<SomeType, bool>> SetDateTimePredicate(
Enums.ComparisonOperator comparison,
DateTime dateTime1,
DateTime dateTime2)
{
switch (comparison)
{
case Enums.ComparisonOperator.IsLessThan:
return p => p.SomeDateProperty < dateTime1;
case Enums.ComparisonOperator.IsLessThanOrEqualTo:
return p => p.SomeDateProperty <= dateTime1;
case Enums.ComparisonOperator.IsGreaterThan:
return p => p.SomeDateProperty > dateTime1;
case Enums.ComparisonOperator.IsGreaterThanOrEqualTo:
return p => p.SomeDateProperty >= dateTime1;
case Enums.ComparisonOperator.IsBetween:
return p => p.SomeDateProperty >= dateTime1
&& p.SomeDateProperty <= dateTime2;
default:
return p => p.SomeDateProperty == dateTime1;
}
}
I tried an extension method but get this error:
System.NotSupportedException occurred
Message="Method 'Boolean Compare(System.DateTime, ComparisonOperator, System.DateTime, System.DateTime)' has no supported translation to SQL."
Extension method:
public static bool Compare(
this DateTime dateToCompare,
Enums.ComparisonOperator comparison,
DateTime dateTime1,
DateTime dateTime2)
{
switch (comparison)
{
case Enums.ComparisonOperator.IsLessThan:
return dateToCompare < dateTime1;
case Enums.ComparisonOperator.IsLessThanOrEqualTo:
return dateToCompare <= dateTime1;
case Enums.ComparisonOperator.IsGreaterThan:
return dateToCompare > dateTime1;
case Enums.ComparisonOperator.IsGreaterThanOrEqualTo:
return dateToCompare >= dateTime1;
case Enums.ComparisonOperator.IsBetween:
return dateToCompare >= dateTime1
&& dateToCompare <= dateTime2;
default:
return dateToCompare == dateTime1;
}
}
Sample with extension method:
somePredicate = somePredicate.Or(
p => p.SomeDateProperty.Compare(comparisonOperator, dateTime1, dateTime2));

Below is a generic solution, that could be used for any type that has the comparison operators defined not just DateTime. I made the methods, extension methods on the Enum.ComparisionOperator type. It would be used like:
Expression<Func<SomeType, bool>> somePredicate = comparisonOperator.Compare(
(SomeType p) => p.SomeDateProperty, dateTime1, dateTime2));
The methods defined as:
public static Expression<Func<TSource, bool>> Compare<TSource, TValue>(
this Enums.ComparisonOperator comparison,
Expression<Func<TSource, TValue>> source,
TValue value1, TValue value2)
{
var value1Expr = Expression.Constant(value1);
var value2Expr = Expression.Constant(value2);
var newExpr = comparison.CompareExpr(source.Body, value1Expr, value2Expr);
return Expression.Lambda<Func<TSource, bool>>(newExpr, source.Parameters);
}
public static Expression CompareExpr(
this Enums.ComparisonOperator comparison,
Expression exprLeft,
Expression exprRight1, Expression exprRight2)
{
switch (comparison)
{
case Enums.ComparisonOperator.IsLessThan:
return Expression.LessThan(exprLeft, exprRight1);
case Enums.ComparisonOperator.IsLessThanOrEqualTo:
return Expression.LessThanOrEqual(exprLeft, exprRight1);
case Enums.ComparisonOperator.IsGreaterThan:
return Expression.GreaterThan(exprLeft, exprRight1);
case Enums.ComparisonOperator.IsGreaterThanOrEqualTo:
return Expression.GreaterThanOrEqual(exprLeft, exprRight1);
case Enums.ComparisonOperator.IsBetween:
return Expression.AndAlso(
Expression.GreaterThanOrEqual(exprLeft, exprRight1),
Expression.LessThanOrEqual(exprLeft, exprRight2));
default:
return Expression.Equal(exprLeft, exprRight1);
}
}
Edit: I changed the methods above to be generic.

Related

Trying to create a morethan, equal or greaterthan dynamic filter for dates in linq

I have been trying to create a expression tree filter for Linq that take 2 dates and a string of possible values { "lessthan", "equals", "morethan" }. I wish to format the call to be something like Query.Where(CompareDates(x => x.left, right, "less than"));
I have the code:
public static IQueryable<TSource> CompareDates<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, DateTime?>> left,
DateTime? right,
string equality)
{
if (right == null || string.IsNullOrWhiteSpace(equality))
return source;
var p = left.Parameters.Single();
Expression member = p;
Expression leftExpression = Expression.Property(member, "left");
Expression rightParameter = Expression.Constant(right, typeof(DateTime));
BinaryExpression BExpression = null;
switch (equality)
{
case "lessthan":
BExpression = Expression.LessThan(leftExpression, rightParameter);
break;
case "equal":
BExpression = Expression.Equal(leftExpression, rightParameter);
break;
case "morethan":
BExpression = Expression.GreaterThan(leftExpression, rightParameter);
break;
default:
throw new Exception(String.Format("Equality {0} not recognised.", equality));
}
return source.Where(Expression.Lambda<Func<TSource, bool>>(BExpression, p));
}
Unfortunately it is producing an error of "System.ArgumentException: Instance property 'left' is not defined for type 'Model' at System.Linq.Expressions.Expression.Property(Expression expression, String propertyName) at SARRestAPI.Extensions.Expressions.CompareDates[TSource](IQueryable1 source, Expression1 src, DateTime supplied, String equality)"
Anyone have an ideas why this is happening?
Here we go; what you want to do is use the .Body of the incoming selector, not look for .left. Meaning, given an input selector of x => x.Foo.Bar.Blap, a constant, and a comparison, you want to construct something like x => x.Foo.Bar.Blap < someValue, by reusing both the x (the parameter, which you're already doing) and the body (x.Foo.Bar.Blap).
In code (note this works for any TValue, not just DateTime):
public enum Comparison
{
Equal,
NotEqual,
LessThan,
LessThanOrEqual,
GreaterThan,
GreaterThanOrEqual
}
public static IQueryable<TSource> Compare<TSource, TValue>(
this IQueryable<TSource> source,
Expression<Func<TSource, TValue>> selector,
TValue value,
Comparison comparison)
{
Expression left = selector.Body;
Expression right = Expression.Constant(value, typeof(TValue));
BinaryExpression body;
switch (comparison)
{
case Comparison.LessThan:
body = Expression.LessThan(left, right);
break;
case Comparison.LessThanOrEqual:
body = Expression.LessThanOrEqual(left, right);
break;
case Comparison.Equal:
body = Expression.Equal(left, right);
break;
case Comparison.NotEqual:
body = Expression.NotEqual(left, right);
break;
case Comparison.GreaterThan:
body = Expression.GreaterThan(left, right);
break;
case Comparison.GreaterThanOrEqual:
body = Expression.GreaterThanOrEqual(left, right);
break;
default:
throw new ArgumentOutOfRangeException(nameof(comparison));
}
return source.Where(Expression.Lambda<Func<TSource, bool>>(body, selector.Parameters));
}
Example usage (here using LINQ-to-Objects, but it should work for other LINQ backends, too):
var arr = new[] { new { X = 11 }, new { X = 12 }, new { X = 13 }, new { X = 14 } };
var source = arr.AsQueryable();
var filtered = source.Compare(x => x.X, 12, Comparison.GreaterThan);
foreach (var item in filtered)
{
Console.WriteLine(item.X); // 13 and 14
}
Note that with C# vCurrent you can do:
var body = comparison switch
{
Comparison.LessThan => Expression.LessThan(left, right),
Comparison.LessThanOrEqual => Expression.LessThanOrEqual(left, right),
Comparison.Equal => Expression.Equal(left, right),
Comparison.NotEqual => Expression.NotEqual(left, right),
Comparison.GreaterThan => Expression.GreaterThan(left, right),
Comparison.GreaterThanOrEqual => Expression.GreaterThanOrEqual(left, right),
_ => throw new ArgumentOutOfRangeException(nameof(comparison)),
};
return source.Where(Expression.Lambda<Func<TSource, bool>>(body, selector.Parameters));
which you might fine preferable.

Order by using Iqueryable for datatable

I have following function that prepare data for jquery datatable grid.
Now I am facing following error for Datatype other than string
Unable to cast the type 'System.DateTime' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
Code:
public GeneralResponse<IEnumerable<Holiday>> GetHolidays(string filter,
int initialPage,
int pageSize,
out int totalRecords,
out int recordFilterd,
int sortColumn,
string sortDirection)
{
var response = new GeneralResponse<IEnumerable<Holiday>>();
totalRecords = 0;
recordFilterd = 0;
filter = filter.Trim().ToLower();
try
{
Expression<Func<Holiday, dynamic>> expr;
switch (sortColumn)
{
case 0:
expr = p => p.HolidayDate;
break;
case 1:
expr = p => p.Name;
break;
case 2:
expr = p => p.ExchangeMarket.Name;
break;
default:
expr = p => p.CreatedOn;
break;
}
var data = holidayRepository.Query(true);
//var data = holidayRepository.GetAll(true).AsQueryable(); previous working one
totalRecords = data.Count();
//filter
if (!string.IsNullOrWhiteSpace(filter))
{
data = data.Where(x => x.Name.ToLower().Contains(filter));
//todo : Add date search as well
}
recordFilterd = data.Count();
//sort
data = sortDirection == "asc" ? data.OrderBy(expr) : data.OrderByDescending(expr);
data = data
.Skip(initialPage * pageSize)
.Take(pageSize);
var result = data.ToList();
response.Data = result;
}
catch (Exception e)
{
response.Error = true;
response.Exception = e;
}
return response;
}
// This method is under generic repository
public IQueryable<T> Query()
{
return Query(false);
}
earlier I was using IEnumerable which was first loading all data in list & then performing filter which was working fine (but not correct or best practice)
Now I am stuck with the filter part. How can I fix this error to have order by all type of property column?
I did lots of research but could not found the any solution.
dynamic or object cannot be used as TKey generic argument of EF Queryable.OrderBy (actually in any Queryable method expression) - it has to be the actual type of the key. Which in turn means you cannot use a common Expression<Func<...>> variable to hold the keySelector expression.
The solution is to use conditional .OrderBy[Descending] inside your switch / case block.
To make handling the ascending/descending option easier (and avoid expression duplication), start by creating a simple custom extension method like this:
namespace System.Linq
{
public static class QueryableExtensions
{
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, bool ascending)
{
return ascending ? source.OrderBy(keySelector) : source.OrderByDescending(keySelector);
}
}
}
Then move the switch / case block after the
recordFilterd = data.Count();
line and use the above helper method inside:
bool ascending = sortDirection == "asc";
switch (sortColumn)
{
case 0:
data = data.OrderBy(p => p.HolidayDate, ascending);
break;
case 1:
data = data.OrderBy(p => p.Name, ascending);
break;
case 2:
data = data.OrderBy(p => p.ExchangeMarket.Name, ascending);
break;
default:
data = data.OrderBy(p => p.CreatedOn, ascending);
break;
}

function with lots of if else's blocks. Can these be removed

I have a function that needs to check a datetime against a supplied datetime. My function is shown below (it works fine however I don't like it). The only thing that needs to change is the operator but currently I have a few if, else if's etc & lines of code that have been copied.
I'm sure I'm being stupid and there is a much better way of doing this?
enum DateTimeOperator
{
Equals = 0, GreaterThanOrEqualTo, GreaterThan, LessThan
}
My function
bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator)
{
if(dateOperator == DateTimeOperator.Equals)
{
if (File.GetLastWriteTime(filePath + fileName).Date == dateCheck .Date)
return true;
else
return false;
}
else if(dateOperator == DateTimeOperator.GreaterThanOrEqualTo)
{
if (File.GetLastWriteTime(filePath + fileName).Date >= dateCheck .Date)
return true;
else
return false;
}
else if(dateOperator == DateTimeOperator.LessThan)
{
if (File.GetLastWriteTime(filePath + fileName).Date < dateCheck .Date)
return true;
else
return false;
}
}
Just for fun without if/switch:
Dictionary<DateTimeOperator, Func<DateTime, DateTime, bool>> operatorComparer = new Dictionary<DateTimeOperator, Func<DateTime, DateTime, bool>>
{
{ DateTimeOperator.Equals, (a, b) => a == b },
{ DateTimeOperator.GreaterThanOrEqualTo, (a, b) => a >= b },
{ DateTimeOperator.GreaterThan, (a, b) => a > b },
{ DateTimeOperator.LessThan, (a, b) => a < b }
};
bool DateChecked(DateTime dateCheck, DateTimeOperator dateOperator)
{
//TODO: add a sanity check
return operatorComparer[dateOperator](File.GetLastWriteTime(filePath + fileName).Date, dateCheck .Date);
}
I'd suggest an extension method: you have no need to put any switch or if whenever you want to compare dates using DateTimeOperator:
public static class DateTimeOperatorExtensions {
public static Func<Boolean, DateTime, DateTime> Comparison(this DateTimeOperator operation) {
switch(operation) {
//TODO: implenent other cases: i.e. DateTimeOperator.NotEquals here
DateTimeOperator.Equals:
return (left, right) => left == right;
DateTimeOperator.GreaterThanOrEqualTo:
return (left, right) => left >= right;
DateTimeOperator.LessThan:
return (left, right) => left < right;
default:
return (left, right) => left == right;
}
}
}
...
bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator) {
return dateOperator.Comparison()(dateCheck, File.GetLastWriteTime(filePath + fileName).Date);
}
Here is my version of the above code, which I believe to be simpler and more readable
bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator)
{
var result = false;
var myDate = File.GetLastWriteTime(filePath + fileName).Date;
switch(dateOperator)
{
case DateTimeOperator.Equals:
result = myDate == dateCheck.Date;
break;
case DateTimeOperator.GreaterThanOrEqualTo:
result = myDate >= dateCheck.Date;
break;
case DateTimeOperator.LessThan:
result = myDate < dateCheck.Date;
break;
}
return result;
}
or if you don't like the one return statement
bool DateChecked(DateTime dateCheck DateTimeOperator dateOperator)
{
var myDate = File.GetLastWriteTime(filePath + fileName).Date;
switch(dateOperator)
{
case DateTimeOperator.Equals:
return myDate == dateCheck.Date;
case DateTimeOperator.GreaterThanOrEqualTo:
return myDate >= dateCheck.Date;
case DateTimeOperator.LessThan:
return myDate < dateCheck.Date;
}
}

DataTables multisort columns .net mvc hint

i'm using Datatables Jquery Extension and i've made multicolum sorting reading those posts:
http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part#Sorting
http://farm-fresh-code.blogspot.it/2012/02/mvc-jquery-ui-and-datatable-pluginajax.html
My question is, those two methods are dependent to The model that i'm using in my Controller, in this case
If i need to use similar code in other controllers i need to replicate that and changing model fields and the Type with others, for example .
This seems to me not very DRY.
How to Proceed? Is controller the good position for those two methods?
private IOrderedQueryable<User> CreateSortedQuery(DataTableParameterModel parameterModel, IQueryable<User> baseQuery)
{
var orderedQuery = (IOrderedQueryable<User>)baseQuery;
for (int i = 0; i < parameterModel.iSortingCols; ++i)
{
var ascending = string.Equals("asc", parameterModel.sSortDir[i], StringComparison.OrdinalIgnoreCase);
int sortCol = parameterModel.iSortCol[i];
Expression<Func<User, string>> orderByExpression = GetOrderingFunc(sortCol);
if (orderByExpression != null)
{
if (ascending)
{
orderedQuery = (i == 0)
? orderedQuery.OrderBy(orderByExpression)
: orderedQuery.ThenBy(orderByExpression);
}
else
{
orderedQuery = (i == 0)
? orderedQuery.OrderByDescending(orderByExpression)
: orderedQuery.ThenByDescending(orderByExpression);
}
}
else
{
if (ascending)
{
orderedQuery = (i == 0)
? orderedQuery.OrderBy(c => c.Id)
: orderedQuery.ThenBy(c => c.Id);
}
else
{
orderedQuery = (i == 0)
? orderedQuery.OrderByDescending(c => c.Id)
: orderedQuery.ThenByDescending(orderByExpression);
}
}
}
return orderedQuery;
}
private Expression<Func<User, string>> GetOrderingFunc(int ColumnIndex)
{
Expression<Func<User, string>> InitialorderingFunction;
switch (ColumnIndex)
{
case 1:
InitialorderingFunction = c => c.FirstName;
break;
case 2:
InitialorderingFunction = c => c.LastName;
break;
case 3:
InitialorderingFunction = c => c.UserName;
break;
case 4:
InitialorderingFunction = c => c.Email;
break;
case 5:
InitialorderingFunction = c => c.BusinessName;
break;
default:
InitialorderingFunction = null;
break;
}
return InitialorderingFunction;
}
I guess, your question is pretty close to these two answers:
Property name evaluating from expression:
public static RouteValueDictionary GetInfo<T,P>(this HtmlHelper html, Expression<Func<T, P>> action) where T : class
{
var expression = (MemberExpression)action.Body;
string fieldName = expression.Member.Name;
and
Applying linq sorting passing string values with LINQ Dynamic Query Library:
var result = data
.Where(/* ... */)
.Select(/* ... */)
.OrderBy(fieldName + " asc");

Dynamically Build Linq Lambda Expression

Okay, my guess is this is answered somewhere already, and I'm just not quite familiar enough with the syntax yet to understand, so bear with me.
The users of my web app need to filter a long list of items in a gridview, accessed via a linqdatasource. I am using the OnSelecting Event to further filter items. I want to filter those items based on selections the users make in DropDownLists.
For example, they select "Title" "Contains" "Fred"
This results in
e.Result = dbContext.Opps.Where(opp => opp.Title.Contains("Fred"));
Or "Description" "Does not Contain" "Alpha"
results in
e.Result = dbContext.Opps.Where(opp => !opp.Description.Contains("Alpha"));
I would like to build up that Expression (System.Linq.Expressions.Expression>) dynamically, instead of having nested switch expressions to generate it, as there are a number of fields I want to check, and I also want to use the StartsWith and EndsWith checks. If I could build the Expression as a string, like so:
string stringExpression = string.Format("opp => opp.{0}.{1}(\"{2}\")",
ddlCustomFilter.SelectedValue,
ddlFilterType.SelectedValue,
txtFilterText.Text);
And then somehow have it be converted into an Expression... is this possible? Or should I just bite the bullet and generate all the switch() statements required to create the various expressions?
You could certainly build the expression dynamically, but I would consider using Dynamic LINQ as an alternative first, though you may not be able to use Contains. In addition, you may want to consider using PredicateBuilder to build complex queries additively.
try this code...
call ToExpression Method()....
public static Expression<Func<T, bool>> ToExpression<T>(string andOrOperator, string propName, string opr, string value, Expression<Func<T, bool>> expr = null)
{
Expression<Func<T, bool>> func = null;
try
{
ParameterExpression paramExpr = Expression.Parameter(typeof(T));
var arrProp = propName.Split('.').ToList();
Expression binExpr = null;
string partName = string.Empty;
arrProp.ForEach(x =>
{
Expression tempExpr = null;
partName = partName.IsNull() ? x : partName + "." + x;
if (partName == propName)
{
var member = NestedExprProp(paramExpr, partName);
var type = member.Type.Name == "Nullable`1" ? Nullable.GetUnderlyingType(member.Type) : member.Type;
tempExpr = ApplyFilter(opr, member, Expression.Convert(ToExprConstant(type, value), member.Type));
}
else
tempExpr = ApplyFilter("!=", NestedExprProp(paramExpr, partName), Expression.Constant(null));
if (binExpr != null)
binExpr = Expression.AndAlso(binExpr, tempExpr);
else
binExpr = tempExpr;
});
Expression<Func<T, bool>> innerExpr = Expression.Lambda<Func<T, bool>>(binExpr, paramExpr);
if (expr != null)
innerExpr = (andOrOperator.IsNull() || andOrOperator == "And" || andOrOperator == "AND" || andOrOperator == "&&") ? innerExpr.And(expr) : innerExpr.Or(expr);
func = innerExpr;
}
catch { }
return func;
}
private static MemberExpression NestedExprProp(Expression expr, string propName)
{
string[] arrProp = propName.Split('.');
int arrPropCount = arrProp.Length;
return (arrPropCount > 1) ? Expression.Property(NestedExprProp(expr, arrProp.Take(arrPropCount - 1).Aggregate((a, i) => a + "." + i)), arrProp[arrPropCount - 1]) : Expression.Property(expr, propName);
}
private static Expression ToExprConstant(Type prop, string value)
{
if (value.IsNull())
return Expression.Constant(value);
object val = null;
switch (prop.FullName)
{
case "System.Guid":
val = value.ToGuid();
break;
default:
val = Convert.ChangeType(value, Type.GetType(prop.FullName));
break;
}
return Expression.Constant(val);
}
private static Expression ApplyFilter(string opr, Expression left, Expression right)
{
Expression InnerLambda = null;
switch (opr)
{
case "==":
case "=":
InnerLambda = Expression.Equal(left, right);
break;
case "<":
InnerLambda = Expression.LessThan(left, right);
break;
case ">":
InnerLambda = Expression.GreaterThan(left, right);
break;
case ">=":
InnerLambda = Expression.GreaterThanOrEqual(left, right);
break;
case "<=":
InnerLambda = Expression.LessThanOrEqual(left, right);
break;
case "!=":
InnerLambda = Expression.NotEqual(left, right);
break;
case "&&":
InnerLambda = Expression.And(left, right);
break;
case "||":
InnerLambda = Expression.Or(left, right);
break;
case "LIKE":
InnerLambda = Expression.Call(left, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), right);
break;
case "NOTLIKE":
InnerLambda = Expression.Not(Expression.Call(left, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), right));
break;
}
return InnerLambda;
}
public static Expression<Func<T, object>> PropExpr<T>(string PropName)
{
ParameterExpression paramExpr = Expression.Parameter(typeof(T));
var tempExpr = Extentions.NestedExprProp(paramExpr, PropName);
return Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Lambda(tempExpr, paramExpr).Body, typeof(object)), paramExpr);
}
public static IQueryOver<T, T> OrderBy<T>(this IQueryOver<T, T> Collection, string sidx, string sord)
{
return sord == "asc" ? Collection.OrderBy(NHibernate.Criterion.Projections.Property(sidx)).Asc : Collection.OrderBy(NHibernate.Criterion.Projections.Property(sidx)).Desc;
}
public static Expression<Func<T, TResult>> And<T, TResult>(this Expression<Func<T, TResult>> expr1, Expression<Func<T, TResult>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, TResult>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
}

Categories