LINQ statement that equal to State IN ('NY','TX'...) SQL Statement

S

Shimon Sim

Is it possible to write LINQ statement (LINQ to SQL) that would result in
query with IN key word.
Like
SELECT FirstNAme, LastName FROM People WHERE State IN ('NY', 'NJ', 'TX') ?

Thanks,
Shimon.
 
N

Nicholas Paldino [.NET/C# MVP]

Shimon,

Yes, it is, but you have to turn it around in a way, and use the
Contains extension method on the set that you want to find the item in, like
so:

// The states.
string[] states = {"NY", "NJ", "TX"};

// The query.
var query = from p in people where states.Contains(p.State) select new {
p.FirstName, p.LastName };
 
S

Shimon Sim

Just did experiment and it worked!!
PeopleDataContext dc = new PeopleDataContext();
var results = from p in dc.People select p;
results = from p in results where states.Contains(p.State); // states is a
collection.

Debugging showed where State IN (@P1,@P2) . perfect!!
 
S

Shimon Sim

Thanks, I got it.

Nicholas Paldino said:
Shimon,

Yes, it is, but you have to turn it around in a way, and use the
Contains extension method on the set that you want to find the item in,
like so:

// The states.
string[] states = {"NY", "NJ", "TX"};

// The query.
var query = from p in people where states.Contains(p.State) select new {
p.FirstName, p.LastName };


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Shimon Sim said:
Is it possible to write LINQ statement (LINQ to SQL) that would result in
query with IN key word.
Like
SELECT FirstNAme, LastName FROM People WHERE State IN ('NY', 'NJ', 'TX')
?

Thanks,
Shimon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top