Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities - Where IN clause in query [duplicate]

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

like image 619
Muhammad Akhtar Avatar asked Aug 16 '10 08:08

Muhammad Akhtar


People also ask

How do you avoid duplicate records in LINQ query?

We can remove all duplicates like this by using GroupBy and Select methods provided by LINQ . First, we group our list so that we will create groups for each name in the List. Then we use Select to only select the first objects in each group. This will result in the removal of the duplicates.

Can we use multiple where clause in LINQ query?

A single query expression may have multiple where clauses.

What does .include do in LINQ?

Introduction to LINQ Include. LINQ include helps out to include the related entities which loaded from the database. It allows retrieving the similar entities to be read from database in a same query. LINQ Include() which point towards similar entities must read from the database to get in a single query.

Does IntelliSense support LINQ Visual Studio?

The C# code editor supports LINQ extensively with IntelliSense and formatting capabilities.


2 Answers

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;
like image 53
Jon Skeet Avatar answered Sep 21 '22 17:09

Jon Skeet


I think answer lies somewhere along these lines...

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

...WHERE a.Contains(ID)
like image 33
Peter Perháč Avatar answered Sep 17 '22 17:09

Peter Perháč