我有这个LINQ查询:
IEnumerable<Pack> pack = from Pack myPack in Packs
select myPack;
返回(例如)34条记录。我只需要选择10到20的记录。我该怎么做?
答案 0 :(得分:12)
使用Skip()
和Take()
的组合:
pack.Skip(10).Take(10);
或者,如果您想要一个声明:
// if you want records 11 - 20 which would be the second set of 10
var packs = Packs.Skip(10).Take(10);
// or if you want records 10 - 20 which would be a set of 11
var packs = Packs.Skip(9).Take(11);
答案 1 :(得分:1)
以下怎么样?我们的想法是将行号投射到每条记录上。这样你就可以在where
子句中使用它。
var pack =
from x in Packs.Select((item, index) => new { RowNum = index + 1, Item = item })
where x.RowNum >= 10 && x.RowNum <= 20
select x.Item;