查看此问题/答案:Entity Framework: Get all rows from the table for the ids in list
现在我的问题是:我想按照 id 列表中的方式对实体进行排序。
我将处理一个小列表,并且不介意从数据库中提取列表后它是否在内存中排序。
最佳答案
var result = db.table
.Where(l => ids.Any(id => id == l.id))
.ToList()
.OrderBy(l => ids.IndexOf(l.id));
或
var result = db.table
.Where(l => ids.Contains(l.id))
.ToList()
.OrderBy(l => ids.IndexOf(l.id));
两者都应该可以正常工作。
关于linq - 通过 id 列表获取实体列表 - 按顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31843807/