Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I truncate a list in C#?

Tags:

I know in python you can do something like myList[1:20] but is there anything similar in C#?

like image 382
FinDev Avatar asked Aug 06 '10 22:08

FinDev


2 Answers

var itemsOneThroughTwenty = myList.Take(20); var itemsFiveThroughTwenty = myList.Skip(5).Take(15); 
like image 132
Tim Robinson Avatar answered Sep 25 '22 00:09

Tim Robinson


You can use List<T>.GetRange():

var subList = myList.GetRange(0, 20); 

From MSDN:

Creates a shallow copy of a range of elements in the source List<T>.

public List<T> GetRange(int index, int count)

like image 35
Dean Harding Avatar answered Sep 24 '22 00:09

Dean Harding