如何在12个项目后停止循环?
public class Program
{
private static void Main(string[] args)
{
var mapper = new Mapper();
foreach (List<int> page in mapper.LoadListPaged(5, 12))
Console.WriteLine($"Start: {page.Min()} --- End: {page.Max()}");
Console.ReadLine();
}
}
public class Mapper
{
private int _index = 0;
public IEnumerable<IEnumerable<int>> LoadListPaged(int pageSize, int limit)
{
int pageCount = 0;
var result = new List<int>();
while (_index < limit)
{
pageCount = 0;
result = new List<int>();
while (pageCount < pageSize)
{
pageCount++;
result.Add(_index++);
}
yield return result;
}
}
}
结果应如下所示:
开始:0 ---结束:4 开始:5 ---结束:9 开始:10 ---结束:12
(请注意,它以14结尾,而不是2)
谢谢!
答案 0 :(得分:1)
while (pageCount < pageSize && _index < limit)