我有这两种类似的方法,我觉得它们可以用表达树替换,传递大于或小于符号
public List<IAccount> IsGreater(DateTime someDate)
{
return AccountList.Where(a =>
a.AccountDate >= someDate && a.SomeBoolMethod() && a.SomeOtherBoolMethod())
}
public List<IAccount> IsLess(DateTime someDate)
{
return AccountList.Where(a =>
a.AccountDate < someDate && a.SomeBoolMethod() && a.SomeOtherBoolMethod())
}
从我读过的关于表达树的内容来看,我觉得这样的事情可能是有序的
Expression<Func<DateTime, ExpressionType, DateTime, bool, bool, bool, List<IAccount>>>
expression = (a, b, c, d, e, f) =>
{
// not sure how to do this here
}
我在附近吗?
答案 0 :(得分:3)
为什么要构建整个表达式树?正如现在提出的问题,你可以直接进行比较。
public List<IAccount> FilterAccounts( Predicate<IAccount> condition )
{
return AccountList.Where(a => condition(a) && a.SomeBoolMethod() && a.SomeOtherBoolMethod() )
}
public List<IAccount> IsGreater(DateTime someDate)
{
return FilterAccounts( a => a.AccountDate >= someDate );
}
public List<IAccount> IsLess(DateTime someDate)
{
return FilterAccounts( a => a.AccountDate < someDate );
}