SQL Statement In to SQL

  • Thread starter Thread starter Baffled
  • Start date Start date
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
 
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
 
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

linq query help 2
Possible in Linq to Sql? 3
a SQL quation 4
How is used Linq on a DataView object? 0
Convert sql to linq? 5
lookup question 1
How to do update sql in LinQ 3
Group By multiple fields in LINQ? 1

Back
Top