How to get the top 30 items on a 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 1000 items and you want to create new lists of 30 items each, and then somehow bind the lists to a list

+6
source share
5 answers

Use the LINQ Take() method:

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

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

+18
source

everyone says linq, so I will show an example without linq:

 List<object> newList = new List<object>(); for(int i=0 ; i < 30 ; i++) newList.Add(oldList[i]); 
+7
source
 newList.AddRange(list.Take(30)); 
+4
source

Use Take (30)

 public List<string> ReturnList(List<string> mylist,int page) { return mylist.Skip(30 * (page - 1)).Take(30) } 
+4
source

use orderby with the column name after that, using .Take(30) , select 30 items from the list.

+2
source

Source: https://habr.com/ru/post/951191/


All Articles