【发布时间】:2009-10-02 12:23:29
【问题描述】:
在方法的签名中,我指定了一个 Func,如下所示:
public void Method (Func<string, bool> func)
在 LINQ 中,哪种方法(来自 IEnumerable)可以让我将方法参数中的 Func 传递给 LINQ 查询?另一个问题是,我的 func 可以有任何类型参数,因此来自 IEnumerable/LINQ 的方法必须支持泛型类型占位符。
我想写这样的东西:
// Get all elements of type T from the webpage (find is an object in an external API to look for elements in a page).
IEnumerable<T> images = find.GetAllByTagName<T>().All(func);
// Where func is a method parameter which is assigned at run time by the consumer of this API:
public void Test (Func<T, bool> func) { }
我怎样才能最好地做到这一点?我在 .NET 3.5 上
谢谢
【问题讨论】:
-
你知道 .All() 的返回类型是布尔值,而不是 IEnumerable,对吧?你可能想要 .Where() 代替。