我有一个DateRange类,我想将它作为where谓词应用于IQueryable,自动使用开始和结束日期并自动使用开放或关闭间隔。
public class DateRange
{
public DateTime? BeginDate { get; set; }
public DateTime? EndDate { get; set; }
public bool BeginInclusive { get; set; }
public bool EndInclusive { get; set; }
public DateRange()
{
BeginInclusive = true;
EndInclusive = false;
}
public IQueryable<T> Apply<T>( IQueryable<T> source, Expression<Func<T,DateTime>> dateField )
{
var result = source;
if (BeginDate.HasValue)
{
if (BeginInclusive)
result = result.Where( x => dateField >= BeginDate ); //does not compile
else
result = result.Where( x => dateField > BeginDate ); //does not compile
}
if (EndDate.HasValue)
{
if (EndInclusive)
result = result.Where( x => dateField <= EndDate ); //does not compile
else
result = result.Where( x => dateField < EndDate ); //does not compile
}
return result;
}
}
我想这样称呼它,DateField是T的任何DateTime属性。
DateRange d;
IQueryable<T> q;
q = d.Apply( q, x => x.DateField );
所以我想将一个成员表达式传递给Apply方法,并让它在结果集中应用一个合适的where子句,但是我无法弄清楚如何在中嵌入dateField成员表达式谓词的表达。请参阅上面的类中的“不编译”行。我需要以某种方式转换dateField或以其他方式构建谓词表达式,但我不知道该怎么做。
答案 0 :(得分:1)
你在这里要做的就是撰写表达方式;你试图将一个表达式应用于另一个表达式的结果。你实际上可以写一个方法来做到这一点:
public static Expression<Func<TSource, TResult>> Compose<TSource, TIntermediate, TResult>(
this Expression<Func<TSource, TIntermediate>> first,
Expression<Func<TIntermediate, TResult>> second)
{
var param = Expression.Parameter(typeof(TSource));
var intermediateValue = first.Body.ReplaceParameter(first.Parameters[0], param);
var body = second.Body.ReplaceParameter(second.Parameters[0], intermediateValue);
return Expression.Lambda<Func<TSource, TResult>>(body, param);
}
它使用以下方法将表达式的参数替换为表达式。
public static Expression ReplaceParameter(this Expression expression,
ParameterExpression toReplace,
Expression newExpression)
{
return new ParameterReplaceVisitor(toReplace, newExpression)
.Visit(expression);
}
public class ParameterReplaceVisitor : ExpressionVisitor
{
private ParameterExpression from;
private Expression to;
public ParameterReplaceVisitor(ParameterExpression from, Expression to)
{
this.from = from;
this.to = to;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return node == from ? to : base.Visit(node);
}
}
这允许您将代码编写为:
public IQueryable<T> Apply<T>(IQueryable<T> source,
Expression<Func<T, DateTime>> dateField)
{
var result = source;
if (BeginDate.HasValue)
{
if (BeginInclusive)
result = result.Where(dateField.Compose(date => date >= BeginDate));
else
result = result.Where(dateField.Compose(date => date > BeginDate));
}
if (EndDate.HasValue)
{
if (EndInclusive)
result = result.Where(dateField.Compose(date => date <= EndDate));
else
result = result.Where(dateField.Compose(date => date < EndDate));
}
return result;
}
答案 1 :(得分:0)
您必须使用Expression类方法手工制作dateField >= BeginDate
。
(...)
if (BeginInclusive)
{
var greaterOrEqual =
Expression.Lambda<Func<T, bool>>(
Expression.GreaterThanOrEqual(
dateField.Body,
Expression.Constant(BeginDate)),
dateField.Parameters);
result = result.Where(greaterOrEqual);
}
(...)
与其他案件类似。
答案 2 :(得分:0)
这是在解决这个问题后创建的更新的Apply方法。
public IQueryable<T> Apply<T>( IQueryable<T> source, Expression<Func<T,DateTime>> dateField )
{
Expression predicate;
if (BeginDate.HasValue)
{
if (BeginInclusive)
predicate = Expression.GreaterThanOrEqual( dateField.Body, Expression.Constant( BeginDate, typeof(DateTime) ) );
else
predicate = Expression.GreaterThan( dateField.Body, Expression.Constant( BeginDate, typeof(DateTime) ) );
source = source.Where( Expression.Lambda<Func<T, bool>>( predicate ) );
}
if (EndDate.HasValue)
{
if (EndInclusive)
predicate = Expression.LessThanOrEqual( dateField.Body, Expression.Constant( EndDate, typeof(DateTime) ) );
else
predicate = Expression.LessThan( dateField.Body, Expression.Constant( EndDate, typeof(DateTime) ) );
source = source.Where( Expression.Lambda<Func<T, bool>>( predicate ) );
}
return source;
}
接下来,我将其转换为扩展方法,因此可以像以下一样使用:
DateRange range;
IQueryable<T> q;
q = q.WhereInDateRange( range, x => x.DateField );