LINQ statement or statement

Does LINQ have an OR operator?

Example:

(controller.Equals("firm").Equals("service").Equals("training"))

How can I make it correspond to the "branded" OR "service" or "training"? thank

+3
source share
3 answers

The easiest way to do what you want is most likely to use a collection of "allowed" words and Contains:

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

var query = from controller in foo.Controllers
            where words.Contains(controller)
            select controller;

Or you can just use:

controller == "firm" || controller == "service" || controller == "training"

in the request.

If this does not help, please give us more context - you gave us only one compilation expression, and not a clear idea of ​​what the query is or what it should do (and whether it is true in LINQ to Objects, LINQ to SQL or what something else).

+9
source

Contains() .

(new[] { "firm", "service", "training" }.Contains(controller))
+3
+2

Source: https://habr.com/ru/post/1788121/


All Articles