所以我有一个类似这样的方法:
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>
关于c# - 您可以将 Expression<Func<T, bool>> 谓词传递到 linq Where 语句中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185534/