Saturday, 28 October 2017

Arrange Distinct Elements Of A List In Ascending Order Using LINQ


Introduction
In this article we will learn how to arrange the distinct element of a List in ascending orde using LINQ. LINQ example to arrange the list element in C#.

Previous Updates
In previous articles we have learnt  Maintain state of dynamic added userConrol in asp.net using c#. Insert Only Numeric values in Asp Textbox using Regex

Description:
Here i am using the List collection to store the temporary/static data. In your case it might be the database data. GetItems function gets all the static data with in Items class. 
Remove Special Character from a String using Regex

class Program
{
static void Main(string[] args)
{
    var itemlist = (from c in Items.GetItems()
                    select c.ItemDes)
               .Distinct()
               .OrderBy(x => x);

    foreach (var item in itemlist)
    Console.WriteLine(item);
    Console.ReadLine();
}
class Items
{
    public int ItemId { getset; }
    public string ItemDes { getset; }

    public static List<Items> GetItems()
    {
        List<Items> itemlist = new List<Items>();
        itemlist.Add(new Items() { ItemId = 1, ItemDes = "Brother" });
        itemlist.Add(new Items() { ItemId = 2, ItemDes = "Jackson" });
        itemlist.Add(new Items() { ItemId = 3, ItemDes = "PointGuruji" });
        itemlist.Add(new Items() { ItemId = 4, ItemDes = "GurujiPoint " });
        itemlist.Add(new Items() { ItemId = 5, ItemDes = "Honey" });
        itemlist.Add(new Items() { ItemId = 6, ItemDes = "Biscuit" });
        return itemlist;
    }
  }
 }

Remove unwanted character from a string Using LINQ

OUTPUT:
Biscuit
Brother
GurujiPoint
Honey
Jackson
PointGuruji

No comments:

Post a Comment