19

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

how can I add WHERE IN statement like...

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

in entity framework

0

2 Answers 2

75

Use Contains:

int[] ids = { 1, 2, 3, 4, 5}; var query = db.myTable.Where(item => ids.Contains(item.ID)); 

or in query syntax:

int[] ids = { 1, 2, 3, 4, 5}; var query = from item in db.myTable where ids.Contains(item.ID) select item; 
Sign up to request clarification or add additional context in comments.

5 Comments

@Jon Skeet thanks. But I have question. I believe you can solve that. That is when ids is being fetching from another table schema and contains some millions data then I can not make this faster. It is being slower for comparison. Is it my thinking or the compiler is giving best optimize run time by managing internal indexing/some other complex algorithm? Please response , I need it now.
@MuhammadAshikuzzaman: In that case you should be doing it with a join instead.
Another thing is that if ids is another model list then I can not use .Contains function what is the way to check .Contains in lambda expression? thanks :)
@MuhammadAshikuzzaman: At this point you should really be asking another question with all the relevant details. (Again, that sounds like you should be using a join...)
Thanks. Here I have make a question. Please see. .Contains Problem
3

I think answer lies somewhere along these lines...

Array a = {1,2,3,4,5} ...WHERE a.Contains(ID) 

1 Comment

and of course, Jon Skeet gives the best answer. And he's FIRST! I wonder how he does it, really. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.