Roger Alsing Weblog

Entity Framework 4 – Where Entity.Id in Array

with 3 comments

Here is a little trick if you want to issue a query to the database and select a batch of entities by ID only:

//assemble an array of ID values
int[] customerIds= new int[] { 1, 2, 3 };

var customers = from customer in context.CustomerSet
                where customerIds.Contains(customer.Id)
                select customer;

This will make Entity Framework issue an SQL query with the where clause “where customerId in (1,2,3)”, and thus, you can batch select specific entities with a single query.
I will get back to this idea in a later post because this is related to how I design my entities and aggregates in DDD.

//Rogerr

Written by Roger Alsing

May 21, 2009 at 7:06 pm

3 Responses

Subscribe to comments with RSS.

  1. Actually this is basic Linq query stuff, although it’s hard to do in a Linq provider (as the set contains works on can be a query as well as the parameter passed into contains ;))

    EF1.0 lacked this Contains operator, which has always surprised me.

    Frans Bouma

    May 22, 2009 at 8:29 am

  2. It is pretty annoying that EF v1 lacks support for this. I agree that it’s basic LINQ stuff (no matter how complicated how complicated this is for a LINQ provider).

    Steven

    May 23, 2009 at 8:26 am

  3. Any way to get this working in EF v1. How would you handle this?

    Bert

    July 14, 2009 at 7:12 am


Leave a Reply