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

Moving back from SQL to LINQ2SQL is sometimes quite simple. The following statement

SELECT user FROM users WHERE lastname='jones' 

easy to carry

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

But how do you get the following SQL?

 SELECT user FROM users WHERE lastname IN ('jones', 'anderson') 
+6
linq linq-to-sql
source share
1 answer

I needed to search a bit 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 
+11
source share

All Articles