Q:

Write a program in C# to display the top n-th records in LINQ Query

belongs to collection: All LINQ programs in C# with examples

0

Write a program in C# to display the top n-th records in LINQ Query

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
class LinqExercise11
{
    static void Main(string[] args)
    {
 
        List<int> list = new List<int>();
        list.Add(1);
        list.Add(2);
        list.Add(3);
        list.Add(4);
        list.Add(5);
        list.Add(6);
        list.Add(7);
        list.Add(8);
        list.Add(9);
        list.Add(10);
 
        Console.WriteLine("\nThe members of the list are : ");
        foreach (var lstnum in list)
        {
            Console.WriteLine(lstnum + " ");
        }
 
        Console.Write("How many records you want to display? : ");
        int num = Convert.ToInt32(Console.ReadLine());
 
        list.Sort();
        list.Reverse();
 
        Console.Write("The top {0} records from the list are: \n", num);
        foreach (int topn in list.Take(num))
        {
            Console.WriteLine(topn);
        }
        Console.ReadLine();
    }
}

Result:

The members of the list are : 

10 

How many records you want to display? : 5

The top 5 records from the list are: 

10

9

8

7

6

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a program in C# to display the number and fr... >>
<< Write a program in C# to find the number of an arr...