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
Use LINQ
Take()
method:
var top30list = source.Take(30).ToList();
Add using System.Linq
at the top of your file to make it work.
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]);
Use Take(30)
public List<string> ReturnList(List<string> mylist,int page)
{
return mylist.Skip(30 * (page - 1)).Take(30)
}
use orderby
with column name after that use as .Take(30)
will select the 30 items from the list.