LINQ Or运算符

Sha*_*eKm 3 linq

LINQ中是否有OR运算符?

例:

(controller.Equals("firm").Equals("service").Equals("training"))
Run Code Online (Sandbox Code Playgroud)

我将如何制作它以匹配'坚定'或'服务'或'培训'?谢谢

Jon*_*eet 9

做你想做的最简单的方法可能是使用一组"允许"的单词,并且Contains:

HashSet<string> words = new HashSet<string> { "firm", "service", "training" };

var query = from controller in foo.Controllers
            where words.Contains(controller)
            select controller;
Run Code Online (Sandbox Code Playgroud)

或者您可以使用:

controller == "firm" || controller == "service" || controller == "training"
Run Code Online (Sandbox Code Playgroud)

在查询中.

如果这没有帮助,请给我们更多的上下文 - 你只给了我们一个非编译表达式,而不是清楚地知道查询是什么或它的意图(事实上它是否在LINQ中对象,LINQ to SQL或其他东西).