我有一个包含成员Predicate
的类,我想在Linq表达式中使用该类:
using System.Linq;
class MyClass
{
public bool DoAllHaveSomeProperty()
{
return m_instrumentList.All(m_filterExpression);
}
private IEnumerable<Instrument> m_instrumentList;
private Predicate<Instrument> m_filterExpression;
}
当我读到“
Predicate<T>
完全等同于Func<T, bool>
”(see here)时,我希望它能起作用,因为All
接受了参数:Func<Instrument, bool> predicate
。但是,我得到了错误:
Argument 2: cannot convert from 'System.Predicate<MyNamespace.Instrument>' to 'System.Type'
有没有办法将谓词转换为该函数将吞咽的参数?
最佳答案
public bool DoAllHaveSomeProperty()
{
return m_instrumentList.All(i => m_filterExpression(i));
}
关于c# - 将谓词<T>转换为Func <T,bool>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25531837/