如何在一个表达式中包含一个表达式? [英] How can I include one expression in another expression?
问题描述
我有一个DateRange类,我想将其作为where谓词应用于IQueryable,自动使用开始和结束日期,并自动使用打开或关闭间隔.
I have a DateRange class that I'd like to apply to an IQueryable as a where predicate, automatically using the begin and end dates and automatically using an open or closed interval.
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属性.
And I want to call it like this, DateField is any DateTime property of T.
DateRange d;
IQueryable<T> q;
q = d.Apply( q, x => x.DateField );
因此,我想将成员表达式传递给Apply方法,并使其将适当的where子句应用于结果集,但是我无法弄清楚如何获取嵌入在 where 中的dateField成员表达式. em>谓词的表达.请参阅上面的类中的不编译"行.我需要以某种方式转换dateField或以其他方式构建谓词表达式,但是我不知道该怎么做.
So I want to pass a member expression to the Apply method, and have it apply an appropriate where clause to the result set, but I cannot figure out how to get the dateField member expression embedded in the where predicate's expression. See lines "do not compile" in class above. I need to transform dateField somehow or build the predicate expression some other way, but I have no idea how to do so.
推荐答案
您要在此处编写表达式;您正在尝试将一个表达式应用于另一个表达式的结果.实际上,您可以编写一种方法来做到这一点:
What you're looking to do here is to compose expressions; you're trying to apply one expression to the result of another. You can actually write a method to do that:
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);
}
它使用以下方法将表达式的参数替换为表达式.
It uses the following method to replace the parameter of an expression with an expression.
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 : node;
}
}
这允许您将代码编写为:
This allows you to write your code as:
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;
}
这篇关于如何在一个表达式中包含一个表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!