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.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. class LinqExercise11
  8. {
  9. static void Main(string[] args)
  10. {
  11.  
  12. List<int> list = new List<int>();
  13. list.Add(1);
  14. list.Add(2);
  15. list.Add(3);
  16. list.Add(4);
  17. list.Add(5);
  18. list.Add(6);
  19. list.Add(7);
  20. list.Add(8);
  21. list.Add(9);
  22. list.Add(10);
  23.  
  24. Console.WriteLine("\nThe members of the list are : ");
  25. foreach (var lstnum in list)
  26. {
  27. Console.WriteLine(lstnum + " ");
  28. }
  29.  
  30. Console.Write("How many records you want to display? : ");
  31. int num = Convert.ToInt32(Console.ReadLine());
  32.  
  33. list.Sort();
  34. list.Reverse();
  35.  
  36. Console.Write("The top {0} records from the list are: \n", num);
  37. foreach (int topn in list.Take(num))
  38. {
  39. Console.WriteLine(topn);
  40. }
  41. Console.ReadLine();
  42. }
  43. }

Result

C# language techstudy.org

     





Related Topics




Popular Posts