CodeGo.net>如何扩展lambda表达式参数中的谓词
作者:互联网
我想在我的方法中扩展expression参数以添加自己的过滤器.我正在尝试执行以下操作,但是语法错误:
public static IList<MyPage> DoSomething<T>(Expression<Func<T, bool>> predicate)
{
return DataStore().GetPages().Where(p => p.PublishDate < DateTime.Now && predicate)
}
编译器抱怨在Visual Studio 2012中抱怨此错误:
Error 29 Operator ‘&&’ cannot be applied to operands of type ‘
bool
‘
and
‘System.Linq.Expressions.Expression<System.Func<T,bool>>
‘
如果扩展为.Where(predicate),首先扩展谓词会更好,然后进行反馈.你会怎么做?
解决方法:
Would extending the predicate first be better then feed if back as .Where(predicate)? How would you do that?
是的,如果我理解您的建议是正确的,则完全一样.您可以像这样链接.Where():
public static IList<MyPage> DoSomething<T>(Expression<Func<T, bool>> predicate)
{
return DataStore().GetPages().Where(p => p.PublishDate < DateTime.Now).Where(predicate);
}
标签:linq,c,net,lambda,expression-trees 来源: https://codeday.me/bug/20191031/1974085.html