我有下面這個函數需要一個id的列表,並搜索數據庫的匹配的人。拆分查詢到多個查詢,然後加入結果
public IQueryable<Person> GetPersons(List<int> list)
{
return db.Persons.Where(a => list.Contains(a.person_id));
}
我之所以需要這個分成四個查詢是因爲查詢不能取超過2100逗號分隔值:
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RPC request. The maximum is 2100.
我怎能名單分成4塊,爲每個列表進行查詢。然後將結果加入一個人名單?
解決
我不想發佈它作爲自己的答案,並從@George達克特的答案拿名氣而去,只顯示解決方案:
public IQueryable<Person> GetPersons(List<int> list)
{
var persons = Enumerable.Empty<Person>().AsQueryable<Person>();
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;
persons = persons.Union(db.Persons.Where(a => row.Contains(a.person_id)));
}
return persons;
}