Introduction
This blog retrieves top tree record from an
array and a list by using following three steps
- Sorting the array and list
- Reverseing the array and list
- Calling the Take function
some functions are listed for different class
used to retrieve list.
Function for array
- Sort
- Revers
Function for List
- Sort
- Revers
Function in Linq
If you are want to retrieves top 3 records
from array, use above mention two functions for array and take functions
linq or if you are want to retrieves record from list call the functions
mentions for list and take function.
see the below examples
For array
int[]
intigerarray = new
int[] { 6, 2, 9, 4, 5, 1, 7, 8, 3 };
Array.Sort(intigerarray);
Array.Reverse(intigerarray);
int[]
integers = intigerarray.Take(3).ToArray();
foreach
(int ind in
integers)
{
Console.WriteLine(ind);
}
For List
List<int>
integerList = new List<int>();
integerList.Add(6);
integerList.Add(2);
integerList.Add(9);
integerList.Add(4);
integerList.Add(5);
integerList.Add(1);
integerList.Add(7);
integerList.Add(8);
integerList.Sort();
integerList.Reverse();
foreach
(int ind in
integerList.Take(3))
{
Console.WriteLine(ind);
}
Output
9
8
7