Where IN clause in LINQ to Entities query

c# entity-framework sql

Question

Possible Duplicates:
Linq to Entities - Sql “IN” clause
How to implement SQL “in” in Entity framework 4.0

How can I add a WHERE IN statement similar to...

SELECT * FROM myTable WHERE ID IN (1,2,3,4,5)

in the entity framework

1
16
5/23/2017 10:30:46 AM

Accepted Answer

Use Contains :

int[] ids = { 1, 2, 3, 4, 5};

var query = db.myTable.Where(item => ids.Contains(item.ID));

Alternatively, in question form:

int[] ids = { 1, 2, 3, 4, 5};

var query = from item in db.myTable
            where ids.Contains(item.ID)
            select item;
66
8/16/2010 8:51:51 AM

Popular Answer

I believe the solution is found somewhere along these lines...

Array a = {1,2,3,4,5}

...WHERE a.Contains(ID)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow