通过实例和它们的使用,有人可以帮助我理解:
这只是一个好奇的问题,我想知道是否有人有一个很好的答案:
在.NET Framework类库中,我们有两个方法:
public static IQueryable<TSource> Where<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate
)
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
Run Code Online (Sandbox Code Playgroud)
他们为什么用Func<TSource, bool>而不是Predicate<TSource>?好像Predicate<TSource>只由List<T>和Array<T>,而Func<TSource, bool>所使用的几乎所有Queryable和Enumerable方法和扩展方法...什么与怎么了?
我是使用谓词的新手,刚学会了如何编写:
Predicate<int> pre = delegate(int a){ a %2 == 0 };
Run Code Online (Sandbox Code Playgroud)
谓词将返回什么,以及编程时它如何有用?
有人能为这三位最重要的代表提供一个很好的解释(希望有例子):
C#开发人员应该注意哪些其他代表?
您多久在生产代码中使用它们?
我有一个带有成员的类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;
}
Run Code Online (Sandbox Code Playgroud)
当我读到" Predicate<T>[...]完全等同于Func<T, bool>"(见这里)时,我希望这可以起作用,因为All它作为参数:Func<Instrument, bool> predicate.
但是,我收到错误:
Argument 2: cannot convert from 'System.Predicate<MyNamespace.Instrument>' to 'System.Type'
Run Code Online (Sandbox Code Playgroud)
有没有办法将谓词转换为该函数将吞下的参数?