Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

EF Linq. How to keep "Where in" sorting

I have a list of IDs and I need to retrieve entities from a database in exact same order, that IDs in the list.

Such query will return wrong order:

List<Guid> listOfIds = targetIds;
var result = await dbContext
      .TargetEnities
      .Where(x => listOfIds.Contains(x.Id))
      .ToListAsync();

I see a way to make requests in a loop and union result, but it looks too expensive.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How to solve that?

>Solution :

Once you have your list in memory, then try this:

var sorted = result.OrderBy(x => listOfIds.IndexOf(x.Id)).ToList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading