Reputation: 55
I am confused with a flow. I have a list of student objects.
List<Student> StudentLists;
Intially i will be having 10 student objects in the list.
There is a button in C# 4.0 winform and when i clicks on the button,
i need to take first 3 student objects from the list and call a wcf service and send these three student object to the wcf service.
I have implemented the wcf call back functionality.
After processing the web service, i will get the call back result for theose 3 student objects.
Each call back may come in different times.
Once i got all the three call back results from webservice, i want to take next 3 available student object and do the same web service call...
I want to do it untill it processes all the 10 items from the lists.
But I know how to take each time 3 objects from the lists. It is like paging.
var students = StudentLists.Skip(skip).Take(3).ToArray();
But I am doing it in each time button click. In each button click, i will take next 3 objects.
Is there any way that to do all these steps without doing button click ?
Upvotes: 1
Views: 1992
Reputation: 14618
Seems like you want to process the students in batches, if this is the case you could write an extension method to do this:
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items, int batchSize)
{
return items.Select((item, inx) => new { item, inx })
.GroupBy(x => x.inx / batchSize)
.Select(g => g.Select(x => x.item));
}
Usage:
foreach (var batch in StudentLists.Batch(3))
{
//Do something with batch
//Process
//Get result etc...
}
Upvotes: 3