How to compare only date part with linq expression?

心已入冬 提交于 2019-12-04 07:23:15

To achieve what you want you can use predicate

WHERE Sent_Datetime >= '20131224' AND Sent_Datetime < '20131225'

so you can use this expression

DateTime currentDate = DateTime.ParseExact("06/01/2008", "dd/MM/yyyy", null);
DateTime nextDate = currentDate.AddDays(1);

Expression ex1 = Expression.GreaterThanOrEqual(
                       propertyExpression, Expression.Constant(currentDate));
Expression ex2 = Expression.LessThan(
                       propertyExpression, Expression.Constant(nextDate));
Expression body = Expression.AndAlso(ex1, ex2);

var predicate = Expression.Lambda<Func<T, bool>>(body, new[] { parameter });

Of course, here you have sargable predicate.

Update

I've created the complete example for you:

public static IQueryable<T> FilterForColumn<T>(this IQueryable<T> queryable, string colName, string searchText)
{
   if (colName != null && searchText != null)
   {
       var parameter = Expression.Parameter(typeof(T), "m");
       var propertyExpression = Expression.Property(parameter, colName);
       System.Linq.Expressions.ConstantExpression searchExpression = null;
       System.Reflection.MethodInfo containsMethod = null;
       // this must be of type Expression to accept different type of expressions
       // i.e. BinaryExpression, MethodCallExpression, ...
       System.Linq.Expressions.Expression body = null;
       Expression ex1 = null;
       Expression ex2 = null;
       switch (colName)
       {
           case "JobID":
           case "FileSize":
           case "TotalFileSize":
               Int64? size = Convert.ToInt64(searchText);
               searchExpression = Expression.Constant(searchText);
               containsMethod = typeof(Int64?).GetMethod("Equals", new[] { typeof(Int64?) });
               body = Expression.Call(propertyExpression, containsMethod, searchExpression);
               break;
           // section for DateTime? properties
           case "PublishDate":
           case "Birth_date":
           case "Anniversary_date":
           case "Profile_Updated_datetime":
           case "CompletedOn":
               DateTime currentDate = DateTime.ParseExact(searchText, "dd/MM/yyyy", null);
               DateTime nextDate = currentDate.AddDays(1);
               ex1 = Expression.GreaterThanOrEqual(propertyExpression, Expression.Constant(currentDate, typeof(DateTime?)));
               ex2 = Expression.LessThan(propertyExpression, Expression.Constant(nextDate, typeof(DateTime?)));
               body = Expression.AndAlso(ex1, ex2);
               break;
           // section for DateTime properties
           case "Created_datetime":
           case "Reminder_Date":
           case "News_date":
           case "thought_date":
           case "SubscriptionDateTime":
           case "Register_datetime":
           case "CreatedOn":
               DateTime currentDate = DateTime.ParseExact(searchText, "dd/MM/yyyy", null);
               DateTime nextDate = currentDate.AddDays(1);
               ex1 = Expression.GreaterThanOrEqual(propertyExpression, Expression.Constant(currentDate));
               ex2 = Expression.LessThan(propertyExpression, Expression.Constant(nextDate));
               body = Expression.AndAlso(ex1, ex2);
               break;
           default :
               searchExpression = Expression.Constant(searchText);
               containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
               body = Expression.Call(propertyExpression, containsMethod, searchExpression);
               break;
       }
       var predicate = Expression.Lambda<Func<T, bool>>(body, new[] { parameter });
       return queryable.Where(predicate);
   }
   else
   {
       return queryable;
   }
}

You can use the SqlFunctions.DateDiff function specifying "day" value as datepart parameter.

var result = entities
    .Where(item =>
        SqlFunctions.DateDiff("day", item.DateProperty, dateToCompare) == 0);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!