转换谓词T.到Func T,bool [英] Converting a Predicate<T> to a Func<T, bool>

查看:62
本文介绍了转换谓词T.到Func T,bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成员为Predicate的类,我想在Linq表达式中使用它:

I have a class with a member Predicate which I would like to use in a Linq expression:

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作为参数.

As I read that "Predicate<T> is [...] completely equivalent to Func<T, bool>" (see here), I would expect this to work, since All takes in as argument: Func<Instrument, bool> predicate.

但是,我得到了错误:

Argument 2: cannot convert from 'System.Predicate<MyNamespace.Instrument>' to 'System.Type'

是否可以将谓词转换为该函数将吞咽的参数?

Is there a way to convert the predicate to an argument that this function will swallow?

推荐答案

public bool DoAllHaveSomeProperty()
{
    return m_instrumentList.All(i => m_filterExpression(i));
}

这篇关于转换谓词T.到Func T,bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆