Write a program in C# to arrange the distinct elements in the list in ascending order 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.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class LinqExercise
  6. {
  7. static void Main(string[] args)
  8. {
  9. var itemlist = (from c in Item_Mast.Getitem()
  10. select c.ItemDes)
  11. .Distinct()
  12. .OrderBy(x => x);
  13.  
  14. foreach (var item in itemlist)
  15. Console.WriteLine(item);
  16. Console.ReadLine();
  17. }
  18. }
  19. class Item_Mast
  20. {
  21. public int ItemId { get; set; }
  22. public string ItemDes { get; set; }
  23.  
  24. public static List<Item_Mast> Getitem()
  25. {
  26. List<Item_Mast> list = new List<Item_Mast>();
  27. list.Add(new Item_Mast() { ItemId = 1, ItemDes = "AUDI" });
  28. list.Add(new Item_Mast() { ItemId = 2, ItemDes = "BMW" });
  29. list.Add(new Item_Mast() { ItemId = 3, ItemDes = "BUGATTI" });
  30. list.Add(new Item_Mast() { ItemId = 4, ItemDes = "HONDA" });
  31. list.Add(new Item_Mast() { ItemId = 6, ItemDes = "FERRARI" });
  32. list.Add(new Item_Mast() { ItemId = 6, ItemDes = "AUDI" });
  33. list.Add(new Item_Mast() { ItemId = 5, ItemDes = "JAGUAR" });
  34. list.Add(new Item_Mast() { ItemId = 6, ItemDes = "FERRARI" });
  35. return list;
  36. }
  37. }

Result

C# language techstudy.org

     





Related Topics




Popular Posts