如何将 Func<T, bool> 转换为 Predicate<T>?

29

是的,我看到了这个问题,但我找不到我的具体问题的答案。

给定一个接受T并返回布尔值的lambda testLambda(我可以将其制作为Predicate或Func,这取决于我)

我需要能够同时使用List.FindIndex(testLambda)(采用Predicate)和List.Where(testLambda)(采用Func)。

有什么想法如何同时实现两者?

4个回答

62

简单:

Func<string,bool> func = x => x.Length > 5;
Predicate<string> predicate = new Predicate<string>(func);

基本上,您可以使用任何兼容的现有实例创建新的委托实例。这也支持协变和逆变:

Action<object> actOnObject = x => Console.WriteLine(x);
Action<string> actOnString = new Action<string>(actOnObject);

Func<string> returnsString = () => "hi";
Func<object> returnsObject = new Func<object>(returnsString);

如果您想将其变成通用的:
static Predicate<T> ConvertToPredicate<T>(Func<T, bool> func)
{
    return new Predicate<T>(func);
}

2
没错。向后兼容性是一个非常重要的问题。 - Jon Skeet
Predicate<T>的构造函数在哪里有说明文档?MSDN似乎只提到可以通过Lambda表达式或方法创建Predicate<T>,从未提到过使用new - gilly3
@gilly3:没有特定的Predicate<T>构造函数 - 它只是一个委托类型,可以像任何其他委托一样构造。 - Jon Skeet
好的,所以这篇MSDN文章(勉强)记录了使用new创建委托。它展示了后来版本.Net的更新语法。但是,Visual Studio告诉我不能在两个委托之间使用此更新的语法(例如Func<T, bool>Predicate<T>)。显然,我必须使用new。在MSDN上几乎没有关于使用委托类型的new的文档 - 我找不到将委托实例作为参数传递的示例。 C#规范中的第7.6.10.5节包含我正在寻找的内容,但这并不令人满意。 - gilly3
@gilly3:是的,这不是一种常用的委托实例化方式。我甚至在我的委托/事件文章中都没有涉及它(虽然我相信它在《C#深入》中有提到 :) - Jon Skeet
显示剩余7条评论

10

我得到了这个:

Func<object, bool> testLambda = x=>true;
int idx = myList.FindIndex(x => testLambda(x));

可以工作,但感觉不太好。


谢谢,你救了我的一天。 - Ollie Strevel

5

我有点晚加入游戏,但我喜欢扩展方法:

public static class FuncHelper
{
    public static Predicate<T> ToPredicate<T>(this Func<T,bool> f)
    {
        return x => f(x);
    }
}

然后你可以像这样使用它:
List<int> list = new List<int> { 1, 3, 4, 5, 7, 9 };
Func<int, bool> isEvenFunc = x => x % 2 == 0;
var index = list.FindIndex(isEvenFunc.ToPredicate());

嗯,我现在看到了FindIndex扩展方法。这可能是一个更加通用的答案。与ConvertToPredicate并没有太大区别。


0

听起来像是一个案例

static class ListExtensions
{
  public static int FindIndex<T>(this List<T> list, Func<T, bool> f) {
    return list.FindIndex(x => f(x));
  }
}

// ...
Func<string, bool> f = x=>Something(x);
MyList.FindIndex(f);
// ...

我喜欢C#3 ...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接