我有一个类,其中有一个成员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>
"时(参见这里),我期望这段代码能够正常工作,因为All
的参数是:Func<Instrument, bool> predicate
。然而,我却收到了以下错误提示:
Argument 2: cannot convert from 'System.Predicate<MyNamespace.Instrument>' to 'System.Type'
有没有一种方法可以将谓词转换为该函数可以接受的参数?
new Func<T, bool>(predicate)
比x => predicate(x)
好多了!谢谢! - florien