What is the linq equivalent to the SQL IN operator

Luca Romagnoli picture Luca Romagnoli · Feb 25, 2010 · Viewed 34.9k times · Source

With linq I have to check if a value of a row is present in an array.
The equivalent of the sql query:

WHERE ID IN (2,3,4,5)

How can I do it?

Answer

David Morton picture David Morton · Feb 25, 2010

.Contains

var resultset = from x in collection where new[] {2,3,4,5}.Contains(x) select x

Of course, with your simple problem, you could have something like:

var resultset = from x in collection where x >= 2 && x <= 5 select x