所以我有一个类似的方法:
public List<T> SomeFunction(Expression<Func<T, bool>> predicate) {
return someList.Where(predicate).ToList();
}
这段代码是不可编译的,因为我无法将谓词传递给linq-Where语句。有没有办法更改谓词以便将其与linq一起使用?
如果使用Enumerable.Where
方法,则需要编译表达式
public List<T> SomeFunction(Expression<Func<T, bool>> predicate) {
return someList.Where(predicate.Compile()).ToList();
}
还要考虑一下你是否真的需要在这里使用表达式。您可以简单地通过Func<T, bool>