我正在尝试根据id列表查找记录。但是当记录被退回时,订单就会丢失
我需要按照records
中收到的顺序返回list
。我该怎么做?
// Select Records from list of id's
public IQueryable<Record> GetRecords(List<int> list)
{
/*
* Linq to SQL has a limit on 2100
*/
var records = Enumerable.Empty<Record>().AsQueryable<Records>();
var limit = 2000;
var result = list.Select((value, index) => new { Index = index, Value = value })
.GroupBy(x => x.Index / limit)
.Select(g => g.Select(x => x.Value).ToList())
.ToList();
foreach (var r in result)
{
var row = r;
records = records.Union(db.Records.Where(a => row.Contains(a.record_id)));
}
return records;
}
答案 0 :(得分:1)
简单的方法就是这样:
records.Select(x=>new {index = list.IndexOf(x.record_id), record = x})
.OrderBy(x=>x.index).Select(x=>x.record);
这是O(n ^ 2)但可能有O(n log n)一个。