我有一个很好的Linq挑战。我想使用FilterFunction过滤Date上的一些数据。它应该像这样工作:
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 =&gt; DateTime?到s =&gt; Bool待评估。我怎样才能让它发挥作用?
感谢您的阅读。 马亭
UDPATE: 我最终得到了这个(感谢rdvanbuuren)
var predicate = Expression.Lambda<Func<Shipment, bool>>(
Expression.GreaterThanOrEqual(
selector.Body,
Expression.Constant(dateFilter.MinDate, typeof (DateTime?))
), selector.Parameters);
答案 0 :(得分:0)
看一看 How to implement method with expression parameter c#
我认为这正是您所需要的,但使用Expression.GreaterThanOrEqual或Expression.LessThanOrEqual。祝你好运!