我希望按照我将 ID 传递给查询的顺序从 Linq 获取查询结果。所以它看起来像这样:
var IDs = new int [] { 5, 20, 10 }
var items = from mytable in db.MyTable
where IDs.Contains(mytable.mytableID)
orderby // not sure what to do here
select mytable;
我希望按照 ID
(5、20、10)的顺序获取项目。
(注意这类似于 this question ,但我想在 Linq 而不是 SQL 中完成)
最佳答案
我只会在 SQL 查询之外执行此操作。在这种情况下,在 SQL Server 上完成它真的没有任何好处。
var items = (from mytable in db.MyTable
where IDs.Contains(mytable.mytableID)
select mytable)
.ToArray()
.OrderBy(x => Array.IndexOf(ids, x.mytableID));
关于c# - 如何按照我提供的 ID 的顺序从 Linq 查询中获取结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2917329/