Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the top 30 items in a list [duplicate]

Tags:

c#

list

how can I get the top 30 items in a list in C# and add it to a new list?

I have a list of about a 1000 items, and want to create new lists, of about 30 items each, and then somehow bind the lists to listbox

like image 369
Bohrend Avatar asked Aug 07 '13 06:08

Bohrend


2 Answers

Use LINQ Take() method:

var top30list = source.Take(30).ToList();

Add using System.Linq at the top of your file to make it work.

like image 111
MarcinJuraszek Avatar answered Oct 21 '22 15:10

MarcinJuraszek


everybody is saying linq so i'll show example without linq:

List<object> newList = new List<object>();

for(int i=0 ; i < 30 ; i++)
    newList.Add(oldList[i]);
like image 43
No Idea For Name Avatar answered Oct 21 '22 15:10

No Idea For Name