Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Display top nth records from the list

C# Sharp LINQ : Exercise-11 with Solution

Write a program in C# Sharp to display the top nth records.

Sample Solution:-

C# Sharp Code:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  


class LinqExercise11 
    {  
        static void Main(string[] args)  
      {  
          
        List<int> templist = new List<int>();
        templist.Add(5);
        templist.Add(7);
        templist.Add(13);
        templist.Add(24);
        templist.Add(6);
        templist.Add(9);
        templist.Add(8);
        templist.Add(7);

        Console.Write("\nLINQ : Display top nth  records from the list : "); 
        Console.Write("\n----------------------------------------------\n");
        
           Console.WriteLine("\nThe members of the list are : ");            
            foreach (var lstnum in templist)  
            {  
                Console.WriteLine(lstnum+" ");  
            }          
        
        Console.Write("How many records you want to display ? : ");  
        int n= Convert.ToInt32(Console.ReadLine()); 

        templist.Sort();
        templist.Reverse();

        Console.Write("The top {0} records from the list are : \n",n);  
        foreach (int topn in templist.Take(n))
            {
                Console.WriteLine(topn);
            }
        }  
    }
	

Sample Output:

----------------------------------------------                                                                
The members of the list are :                                                                                 
5                                                                                                     
7                                                                                                     
13                                                                                                     
24                                                                                                     
6                                                                                                     
9                                                                                                     
8                                                                                                     
7                                                                                                     
How many records you want to display ? : 5                                                                    
The top 5 records from the list are :                                                                         
24                                                                                                     
13                                                                                                     
9                                                                                                     
8                                                                                                     
7  

Pictorial Presentation:

C# Sharp LINQ: Display top nth records from the list.

Flowchart:

Flowchart: LINQ : Display top nth  records from the list

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to Accept the members of a list through the keyboard and display the members more than a specific value.
Next: Write a program in C# Sharp to find the uppercase words in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.