Q:

Write a program in C# to arrange the distinct elements in the list in ascending order in LINQ Query

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

0

Write a program in C# to arrange the distinct elements in the list in ascending order 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.Linq;
using System.Collections.Generic;
 
class LinqExercise
{
    static void Main(string[] args)
    {
        var itemlist = (from c in Item_Mast.Getitem()
                        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> Getitem()
    {
        List<Item_Mast> list = new List<Item_Mast>();
        list.Add(new Item_Mast() { ItemId = 1, ItemDes = "AUDI" });
        list.Add(new Item_Mast() { ItemId = 2, ItemDes = "BMW" });
        list.Add(new Item_Mast() { ItemId = 3, ItemDes = "BUGATTI" });
        list.Add(new Item_Mast() { ItemId = 4, ItemDes = "HONDA" });
        list.Add(new Item_Mast() { ItemId = 6, ItemDes = "FERRARI" });
        list.Add(new Item_Mast() { ItemId = 6, ItemDes = "AUDI" });
        list.Add(new Item_Mast() { ItemId = 5, ItemDes = "JAGUAR" });
        list.Add(new Item_Mast() { ItemId = 6, ItemDes = "FERRARI" });
        return list;
    }
}

Result:

AUDI

BMW

BUGATTI

FERRARI

HONDA

JAGUAR

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 generate a Cartesian Prod...