我是 linq 的新手,想通过扩展 IQueryable 来更深入地研究。我想创建一个查询日期范围的简单扩展方法。等同于:
IQuerable.Where(x => x.Date > fromDate && x.Date < toDate);
但对日期进行了一些额外处理。我希望能够解析一个属性来执行查询,因此方法调用将类似于:
IQueryable.WhereDateRange(x => x.Date, fromDate, toDate);
IQueryable.WhereDateRange(x => x.AnotherDate, fromDate, toDate);
我一直在研究类似下面的内容,但我不完全确定我在做什么
public static IQueryable<T> WhereDateRange<T>(this IQueryable<T> source, Func<T, DateTime> getter, DateTime from, DateTime to) {
//query here
}
这可能吗?如果可以,我该怎么做?
请您参考如下方法:
未经测试,但是:
public static IQueryable<T> WhereDateRange<T>(
this IQueryable<T> source, Expression<Func<T, DateTime>> getter,
DateTime from, DateTime to)
{
Expression body = getter.Body;
var predicate = Expression.Lambda<Func<T, bool>>(
Expression.And(
Expression.GreaterThan(body, Expression.Constant(from)),
Expression.LessThan(body, Expression.Constant(to))
),
getter.Parameters);
return source.Where(predicate);
}
不过,仅供引用;我通常假设范围是 >= start和 < end ,所以我会使用 Expression.GreaterThanOrEqual .
使其更具可重用性(并注意我在这里使用 GreaterThanOrEqual):
public static IQueryable<T> WhereInRange<T, TValue>(
this IQueryable<T> source, Expression<Func<T, TValue>> selector,
TValue from, TValue to)
{
Expression body = selector.Body;
var predicate = Expression.Lambda<Func<T, bool>>(
Expression.And(
Expression.GreaterThanOrEqual(
body, Expression.Constant(from, typeof(TValue))),
Expression.LessThan(
body, Expression.Constant(to, typeof(TValue)))
),
selector.Parameters);
return source.Where(predicate);
}
(在这种情况下需要在常量上指定 TValue,因为 null 否则会导致大问题)
