See this question/answer: Entity Framework: Get all rows from the table for the ids in list
Now my question: I would like to get the entities sorted as they are in the list of id's.
I would be dealing with a small list, and don't mind if it's sorted in memory after pulling list from db.
var result = db.table
.Where(l => ids.Any(id => id == l.id))
.ToList()
.OrderBy(l => ids.IndexOf(l.id));
or
var result = db.table
.Where(l => ids.Contains(l.id))
.ToList()
.OrderBy(l => ids.IndexOf(l.id));
both should work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With