views:

100

answers:

2

I need to split a list into two equal lists.

For Example:

  1. I have a list which consists of 10 items. I need to split the list into two equal parts(each with 5 items)

  2. I have a list which consists of 9 items sometimes. I need to split the list into two parts(one with 5 items and other with 4 items)

Please suggest a solution for this.

A: 

Use Skip and Take


int firstPartCount = originalList.Count() / 2;

var firstPart = originalList.Take(firstPartCount);
var secondPart = originalList.Skip(secondPartCount);
Mike Two
+2  A: 
var list1 = originalList.Take((int)originalList.Count()/2);
var list2 = originalList.Skip(list1.Count());
Mel Gerats
Worked great. Thanks
Prasad