Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I return entities from delimited list of Ids in EF5 Code First

I want to hydrate a collection of entities by passing in a comma delimited list of Ids using EF5 Code First.

I would previously have created a table function in t-sql, passed in the comma delimited list of Ids, I'd then join this table to the target table and return my collection of records.

What is the most performant way of using EF5 Code First to achieve the same?

Update: I want to avoid having the full set of entities in memory first.

Update2: I'd ideally like the order of the entities to match the delimited list.

like image 418
SeanR Avatar asked Feb 18 '23 08:02

SeanR


1 Answers

I'd say to start out by converting the comma delimited list into a List<int> that contains all of the IDs that you would be going for. At that point, using your particular DbContext class you would do the following:

var entities = db.MyEntities.Where(e => myListOfIds.Contains(e.ID)).ToList();

Note: I only put the ToList at the end there because you were talking about hydrating the collection. Otherwise, with IEnumerable, there will be deferred execution of the query, and so it will not populate right away.

like image 190
Corey Adler Avatar answered Apr 06 '23 16:04

Corey Adler