Display Top 3 Records in Linq

Introduction
 
This blog retrieves top tree record from an array and a list by using following three steps
  1. Sorting the array and list
  2. Reverseing the array and list
  3. Calling the Take function
some functions are listed for different class used to retrieve list.
 
Function  for array 
  1. Sort
  2. Revers
Function for List
  1. Sort
  2. Revers
Function in Linq
  1. Take
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);

}


Output
 
8
7
 
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
8
7
Ebook Download
View all
Learn
View all