Reputation: 59491
I need to split a list into two equal lists.
For Example:
I have a list which consists of 10 items. I need to split the list into two equal parts(each with 5 items)
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
Reputation: 2252
var list1 = originalList.Take((int)originalList.Count()/2);
var list2 = originalList.Skip(list1.Count());
Upvotes: 3
Reputation: 46173
Use Skip and Take
int firstPartCount = originalList.Count() / 2;
var firstPart = originalList.Take(firstPartCount);
var secondPart = originalList.Skip(secondPartCount);
Upvotes: 1