www.allpaul.com

I needed somewhere to put all this crap
Welcome to www.allpaul.com Sign in | Help
in Search

Programming

Linq to entities "IN" query

Recently I was looking for a way to build the following SQL query using the entity framework 

   1:  SELECT * FROM tbl_Registrations WHERE [ID] IN (1, 2, 3, 4, 5)

But, turns out, that is not very easy. I thought of three different approaches. To begin, I have a entity set Registrations of entities Registration.  These Registration entities have a property ID (int).  So I tried,

1. Create an int array of the IDs and use the Intersect operator.  But, and there may be other reasons, the Intersect operator doesn't accept Lambda expressions and without it the result would only be a list of ints, which I already had.

2. Create an int array (Or List<int> or List<MyTypeWithIntProperty>) then join the array/list to my Registrations on the ID equal to the value in the array or list.  That doesn't work.

3. Use the Any operator with the same list/array from #2 with a Lambda expression.  That didn't work either.

Normally, I would then just hit the newsgroup and do a search, but I couldn't search on the word IN and the word Any yielded more than 2000 results. After plugging through the almost random search results I finally came across another gem from Colin Meek. The post shows how to achieve the desired results using a utility method to construct an expression of concatenated or statements.  So, my original query gets translated into

      (r.ID == 1 || r.ID == 2 || r.ID == 3 ...)

To clarify HOW to implement his example, I did the following

   1:  int[] regIds = { 1, 2 };
   2:   
   3:  var regs = (from r in se.Registrations
   4:                          select r)
   5:             .Where(Extensions.
   6:             BuildContainsExpression<Registration, int>(e => e.ID, regIds));

 

It should be noted that I have a class that contains all my extension and helper methods called Extensions. Hopefully, by me writing about this it will help anyone looking for way to implement a "IN" query in Linq to entities.

Good luck

 

Only published comments... Sep 15 2008, 08:02 PM by paully21

Comments

 

mnl said:

Thank you very much indeed!!!

February 26, 2009 8:08 AM
 

Bill said:

Any way to implement in VB? I've got the BuildContainsExpression in vb but I can't get my head around the call.

March 5, 2009 2:31 PM
 

Gusman said:

I have tried the above method, but it really doesn't creates an IN query, it creates a chain of OR queries, and it's not fast as a true IN.

I have tried to create an eSQL query with an IN query, but it's queried to the database as a or chain!

Ex.: clients.Where("it.ec_id IN {3,4,5,6}");

SQL result:

SELECT

1 AS [C1],

[Extent1].[ec_id] AS [ec_id],

[Extent1].[ec_nombre] AS [ec_nombre],

[Extent1].[ec_cif] AS [ec_cif],

[Extent1].[ec_provincia] AS [ec_provincia],

[Extent1].[ec_poblacion] AS [ec_poblacion],

[Extent1].[ec_cp] AS [ec_cp],

[Extent1].[ec_calle] AS [ec_calle],

[Extent1].[ec_patio] AS [ec_patio],

[Extent1].[ec_puerta] AS [ec_puerta],

[Extent1].[ec_superadmin] AS [ec_superadmin],

[Extent1].[ec_superpass] AS [ec_superpass],

[Extent1].[ec_licencias] AS [ec_licencias],

[Extent1].[ec_licencias_usadas] AS [ec_licencias_usadas],

[Extent1].[ec_empresa_distribuidora] AS [ec_empresa_distribuidora]

FROM [dbo].[empresa_cliente] AS [Extent1]

WHERE ([Extent1].[ec_id] = 3) OR ([Extent1].[ec_id] = 4) OR ([Extent1].[ec_id] = 5) OR ([Extent1].[ec_id] = 6)

It's a pain....

May 26, 2009 1:17 PM
 

Murtaza said:

Thank You ! Keep it up (Y)

July 20, 2009 9:16 PM
 

Victor said:

When I use this approach, I get this error:

"Query builder methods are not supported for LINQ to Entities queries. For more information, see the Entity Framework documentation."

December 28, 2009 7:05 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About paully21

 

Powered by Community Server (Non-Commercial Edition), by Telligent Systems