我希望按照我将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));