如何通过IEnumerable的循环分批 [英] How to loop through IEnumerable in batches
问题描述
我正在开发有一个IEnumerable的用户存储400万用户的ID C#程序。我需要遍历Ienummerable并提取了一批1000每次在另一种方法执行某些操作的ID。
I am developing a c# program which has an "IEnumerable users" that stores the ids of 4 million users. I need to loop through the Ienummerable and extract a batch 1000 ids each time to perform some operations in another method.
我如何在一次抽取1000 IDS从IEnumerable的开始......做一些别的事情,然后取1000下一批等等?
How do I extract 1000 ids at a time from start of the Ienumerable ...do some thing else then fetch the next batch of 1000 and so on ?
这可能吗?
推荐答案
听起来像是你需要使用跳过并把你的对象的方法。例如:
Sounds like you need to use Skip and Take methods of your object. Example:
users.Skip(1000).Take(1000)
这将跳过第1000和拍摄下1000你只需要增加用量跳过每次调用
this would skip the first 1000 and take the next 1000. You'd just need to increase the amount skipped with each call
您可以使用一个整型变量与跳过参数,并可以调整多少被跳过。然后,您可以调用它的方法。
You could use an integer variable with the parameter for Skip and you can adjust how much is skipped. You can then call it in a method.
public IEnumerable<user> GetBatch(int pageNumber)
{
return users.Skip(pageNumber * 1000).Take(1000);
}
这篇关于如何通过IEnumerable的循环分批的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!