如何在循环中使用.Take()方法批量记录 [英] How Do I Use .Take() Method In A Loop To Take Records In Batches
本文介绍了如何在循环中使用.Take()方法批量记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个包含数千条记录的列表项,我需要将这些记录推送到一批5条记录中的Web服务每次/迭代,即第一次迭代中的1-5个记录,6-10,11-15,16-20。我正在将列表作为方法的参数传递。
Hello everyone,
I have a list item the contains a thousands records, i am required to push these records to a web service in a batch of 5 records per time/iteration, i.e. 1-5 records in the first iteration the 6-10,11-15,16-20. etc. i am passing the list as an argument to a method.
var postData = new DataImportClient();
postData.postDocumentAttribute(cdAttrlist.Take(5).ToArray());
无论如何这是可以实现的还是我可以在检索记录时使用sql语句来实现这个
Please is there anyway this can be achieved or is there sql statement i can use while retrieving the records to achieve this
推荐答案
你需要同时使用Skip()和Take()。
所以你会有一些东西:
You need to use Skip() and Take() in tandem.
so you would have something along the lines :
public IEnumerable<string> GetValues(int pageIndex)
{
return cdAttrlist.Skip(pageIndex * 5).Take(5).ToArray();
}</string>
查看我的文章:有效使用LINQ扩展方法的注意事项 [ ^ ]我的示例任务就是这样。
See my article: Considerations on Efficient use of LINQ Extension Methods[^] where my example task is exactly this.
这篇关于如何在循环中使用.Take()方法批量记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文