小编Maz*_*z H的帖子

传入Func <T,bool>到LINQ Where

嗨我有一个简单的存储库层,其方法如下:

public Questionnaire GetQuestionnaire(Expression<Func<Question, bool>> predicateFunc)
    {
        return new Questionnaire
        {
            Title = "Geography Questions",
            Questions = GetQuestionnaireData().Where(predicateFunc.Compile()).ToList()
        };
    }
Run Code Online (Sandbox Code Playgroud)

这是查询名为'GetQuestionnaireData()'的内存中的IEnumerable集合

但是从上面的方法签名我使用Expression> predicateFunc.然后我通过predicateFunc.Compile在linq查询的where子句中使用它.

我的问题是:是否可以在不使用Expression和delegate.compile的情况下将Func委托传递给linq where子句?

也许这可能是使用@ IEnumerable.Select(somePredicateFunc)?

c# linq expression func where

3
推荐指数
1
解决办法
1146
查看次数

标签 统计

c# ×1

expression ×1

func ×1

linq ×1

where ×1