我有一个很好的Linq挑战。我想使用FilterFunction过滤日期上的一些数据。它应该像这样工作:
ApplyDateFilterEx(query,null, DateTime.Today, s => s.CreatedDate);
应该过滤查询,以便返回直到今天的所有“货件”。查询对象是IQueryable,整个查询应由EF5评估,因此应转换为SQL。
这是我到目前为止(无法编译)的内容:
private static IQueryable<Shipment> ApplyDateFilterEx(IQueryable<Shipment> query, DateTime? minDate, DateTime? maxDate, Expression<Func<Shipment, DateTime?>> dateMember)
{
if (minDate != null)
{
//convert func to expression so EF understands
Expression<Func<Shipment, bool>> where = x => minDate <= dateMember(x);
query = query.Where(where);
}
if (maxDate != null)
{
Expression<Func<Shipment, bool>> where = x => dateMember(x) <= maxDate;
query = query.Where(where);
}
return query;
}
您可以看到我要转换表达式s => DateTime吗?到s =>要评估的布尔。我怎样才能使它正常工作?
感谢您的阅读。马丁
UDPATE:我结束了(感谢rdvanbuuren)
var predicate = Expression.Lambda<Func<Shipment, bool>>(
Expression.GreaterThanOrEqual(
selector.Body,
Expression.Constant(dateFilter.MinDate, typeof (DateTime?))
), selector.Parameters);
我认为这正是您所需要的,但是具有Expression.GreaterThanOrEqual或Expression.LessThanOrEqual。祝你好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句