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: Arrange distinct elements in the list in ascending order

C# Sharp LINQ : Exercise-30 with Solution

Write a program in C# Sharp to arrange the distinct elements in the list in ascending order.

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;
using System.Collections.Generic;
 
class  LinqExercise30
{
    static void Main(string[] args)
    {
            Console.Write("\nLINQ : Arrange distinct elements in the list in ascending order : "); 
            Console.Write("\n----------------------------------------------------------------\n");	

         var itemlist = (from c in Item_Mast.GetItemMast()
					select c.ItemDes)
					.Distinct()
					.OrderBy(x => x);
					
					foreach(var item in itemlist)
					Console.WriteLine(item);
					Console.ReadLine();
    }
}
class Item_Mast
{
    public int 	ItemId { get; set; }
    public string ItemDes { get; set; }

   public static List<Item_Mast> GetItemMast()
    {
        List<Item_Mast> itemlist = new List<Item_Mast>();
        itemlist.Add(new Item_Mast() { ItemId = 1, ItemDes = "Biscuit  " });
        itemlist.Add(new Item_Mast() { ItemId = 2, ItemDes = "Honey    " });
        itemlist.Add(new Item_Mast() { ItemId = 3, ItemDes = "Butter   " });
        itemlist.Add(new Item_Mast() { ItemId = 4, ItemDes = "Brade    " });
        itemlist.Add(new Item_Mast() { ItemId = 5, ItemDes = "Honey    " });
        itemlist.Add(new Item_Mast() { ItemId = 6, ItemDes = "Biscuit  " });
        return itemlist;
    }
}

Sample Output:

LINQ : Arrange distinct elements in the list in ascending order :                                             
----------------------------------------------------------------                                              
Biscuit                                                                                                       
Brade                                                                                                       
Butter                                                                                                       
Honey

Flowchart:

Flowchart: LINQ : Arrange distinct elements in the list in ascending order

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to split a collection of strings into some groups.
Next: C# Sharp STRUCTURE Exercises.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.