Linq version of SQL IN

Posts   
 
    
Posts: 1
Joined: 29-Jan-2010
# Posted on: 29-Jan-2010 19:01:53   

Hello,

I wanted to write a LINQ query corresponding to SQL IN operator. My Query is:

Select * from Table1 where someID in (select ID from TABLE2 where category == 5)

The problem is that the field 'someID' is defined in the database as a NULLABLE field. I can't change the schema of t he database.

I searched the online forums and was able to write the following LINQ Query:

var q = from m1 in metaData.Table1 where (from m2 in metaData.Table2 where (m2.category == 5) select m2.ID).Contains(m1.someID.GetValueOrDefault()) select m1;

This gives me an error that the argument passed in Contains is not an entity or object.

Please help me if someone has already encountered this problem!

Thanks in advance!

Sachin

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 30-Jan-2010 08:09:27   

Hi Sachin,

The operand of the Contains must be a query result in this case. For example:

var q = from c in metaData.Customer
     where c.Orders.Contains((from o in metaData.Order where o.EmployeeId == 2 select o).First())
     select c;
David Elizondo | LLBLGen Support Team