Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
2.67/5 (2 votes)
See more:
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.

C#
var postData = new DataImportClient();
postData.postDocumentAttribute(cdAttrlist.Take(5).ToArray());


Please is there anyway this can be achieved or is there sql statement i can use while retrieving the records to achieve this
Posted

You need to use Skip() and Take() in tandem.

so you would have something along the lines :

C#
public IEnumerable<string> GetValues(int pageIndex)
{
    return cdAttrlist.Skip(pageIndex * 5).Take(5).ToArray();
}</string>
 
Share this answer
 
Comments
Uwakpeter 30-Jun-14 4:59am    
I have this: var postData = new DataImportClient();
postData.postDocumentAttribute(cdAttrlist.Take(5).ToArray()); in a foreach loop, how do i call this method in the foreach loop? cdAttrlist has to be in the foreach loop for it to bind all records.
Matt T Heffron 30-Jun-14 13:19pm    
This is **VERY** inefficient.
See my article I reference in my Solution 2 for why it is inefficient and how to do it well.
See my article: Considerations on Efficient use of LINQ Extension Methods[^] where my example task is exactly this.
 
Share this answer
 
Comments
Uwakpeter 1-Jul-14 4:52am    
Thanks sir for your contributions, your solution was pretty long, but i have been to implement it, i use offset and fetch next keywords in sql server 2012 to do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900