How will you use Take and Skip extension method to judiciously do batch operation?

 Posted by Niladri.Biswas on 7/18/2012 | Category: C# Interview questions | Views: 1417 | Points: 40
Answer:

Say we have a bulk of records(take 40 for example sake). Now let us do a batch operation for 10 records each time. The program will be

var intList = new List<int>();

Enumerable.Range(1, 40).ToList().ForEach(i => intList.Add(i));

int setRecordCount = 10;

//Pick the records from 1 - 10
var record1To10 = intList.Skip(setRecordCount * 0).Take(setRecordCount);

//Pick the records from 11 - 20
var record11To20 = intList.Skip(setRecordCount * 1).Take(setRecordCount);

//Pick the records from 21 - 30
var record21To30 = intList.Skip(setRecordCount * 2).Take(setRecordCount);

//Pick the records from 31 - 40
var record31To40 = intList.Skip(setRecordCount * 3).Take(setRecordCount);


In the first case, we are skipping no records and picking up the first 10 records from the original 40 records. In the second case, we are first skipping the 10 record
from the entire 40 records. So from the remaining 30 records(i.e. 11 to 40), we are picking up the first 10 records i.e. 11 to 20.

A similar explanation follows for the rest of the two cases also.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response