Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the IN operator in linq

I'm querying a view and filtering the results with a column named status. I'd like to query it so I can search for rows with different status, by using the IN operator as I'd do in SQL.

As so:

SELECT * FROM VIEW WHERE Status in ('....', '.....')

How can I achieve this?

like image 681
Hallaghan Avatar asked Jun 11 '10 15:06

Hallaghan


People also ask

What is the LINQ equivalent to the SQL IN operator?

An IEnumerable<T>.

What are LINQ operators?

A set of extension methods forming a query pattern is known as LINQ Standard Query Operators. As building blocks of LINQ query expressions, these operators offer a range of query capabilities like filtering, sorting, projection, aggregation, etc.

What does LINQ use to implement the query operators?

The LINQ to Objects provider is used for in-memory collections, using the local query execution engine of LINQ. The code generated by this provider refers to the implementation of the standard query operators as defined on the Sequence pattern and allows IEnumerable<T> collections to be queried locally.


1 Answers

If your query expression uses the Contains method of an IEnumerable object, the parser will turn that into an IN expression using the values in the IEnumerable.

List<string> foo = new List<string>() { "a", "b", "c" };

var query = dataContext.View.Where(v => foo.Contains(v.Status));
like image 99
Adam Robinson Avatar answered Oct 14 '22 23:10

Adam Robinson