Prasad
Prasad

Reputation: 59491

split generic list

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.

Upvotes: 0

Views: 356

Answers (2)

Mel Gerats
Mel Gerats

Reputation: 2252

var list1 = originalList.Take((int)originalList.Count()/2);
var list2 = originalList.Skip(list1.Count());

Upvotes: 3

Mike Two
Mike Two

Reputation: 46173

Use Skip and Take


int firstPartCount = originalList.Count() / 2;

var firstPart = originalList.Take(firstPartCount);
var secondPart = originalList.Skip(secondPartCount);

Upvotes: 1

Related Questions