Write a C# program to check whether a given substring is present in the given string

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

Introduction

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

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

Add a Comment

Your email address will not be published. Required fields are marked *