SQL Statement In to SQL

B

Baffled

Hi All,

How would I convert the SQL IN statement to LinQ

Select *
from TableName
where FieldName in (Value1, Value2)

or

Select *
from Table1
where Field1 in (Select Field1 from Table2)

Thanks
 
J

Jon Skeet [C# MVP]

Baffled said:
How would I convert the SQL IN statement to LinQ

Select *
from TableName
where FieldName in (Value1, Value2)

or

Select *
from Table1
where Field1 in (Select Field1 from Table2)

Try the Contains() operator. I haven't tried it for the latter type of
subquery, but I've seen it work for the first type where you pass in
the values.

Just remember that it's the other way round, so:

set.Contains(value) ~= value in set
 
A

Alberto Poblacion

Baffled said:
How would I convert the SQL IN statement to LinQ

Select *
from TableName
where FieldName in (Value1, Value2)

or

Select *
from Table1
where Field1 in (Select Field1 from Table2)


As to the second one, it can be refactored into a Join:

Select Table1.* from Table1 Join Table2 on Table1.Field1=Table2.Field1

This can then be readily converted into a LINQ query with a Join.
 

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

Similar Threads


Top