列表将某些项目跳过剩余

时间:2014-01-29 16:52:12

标签: c# linq list lambda

假设我们有一个包含156个项目的列表,我们需要使用take和skip方法分50个批次,我已完成下面的代码,但不知道如何跳过以前的项目,如果其余代码是正确的:

var mailAccounts = mailRepo.GetAllMailAccounts();
            int total = mailAccounts.Count;
            int batch = 50;
            int batchNumbers = total / batch;
            for (int i = 0; i < batchNumbers; i++)
            {
                mailAccounts.Skip(Previous Items).Take(batch);
                await FetchEmails(mailAccounts);
            }

3 个答案:

答案 0 :(得分:5)

您可以轻松计算出来:

int itemsToSkip = batch * i;
mailAccounts.Skip(itemsToSkip).Take(batch);

答案 1 :(得分:4)

for (int i = 0; i < batchNumbers; i++) {
     mailAccounts.Skip(i * batch).Take(batch);
}

答案 2 :(得分:0)

var chunks =  mailAccount.Select((m, i) => new {m, i})
                         .GroupBy(item = item.i / 50)
                         .Select(g => g.Select(item => item.m));

foreach(var ch in chunks)
{
   var curChunk = ch.ToArray();
}