Jedidja
Jedidja

Reputation: 16960

How do you generate a WHERE...IN clause using LINQ2SQL?

Going backwards from SQL to LINQ2SQL is sometimes quite simple. The following statement

SELECT user FROM users WHERE lastname='jones'

translates fairly easily into

from u in users where u.lastname='jones' select u

But how do you get the following SQL generated?

SELECT user FROM users WHERE lastname IN ('jones', 'anderson')

Upvotes: 9

Views: 1723

Answers (1)

Jedidja
Jedidja

Reputation: 16960

I had to do a bit of searching to find this, and thought it might be useful to others.

List<string> names = new List<string>() { "jones", "anderson" };

from u in users where names.Contains(u.lastname) select u

Upvotes: 13

Related Questions