You must Sign In to post a response.
  • Category: LINQ

    Need Linq Query for the below requirement..

    Hi,

    I have a requirement to sort the List<> object in a customized way.

    I have written requirement in code comments.

    I have written junk code which will give the desired result but its not optimal and robost. Can any one help me with this?

    I have pasted the console application class file which will execute without any errors.

    I have updated the code to add the remaining items in collection to the last of list.

    Thank you.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;

    namespace SortOption
    {
    class Program
    {
    static void Main(string[] args)
    {
    List<clsSort> list = new List<clsSort>() {new clsSort{ AccountType = "SV|40|01", Description = "SV b 40 DATA"},
    new clsSort{ AccountType = "SV|00|20", Description = "SV a 00 DATA"},
    new clsSort{ AccountType = "ER|31|01", Description = "ER 31 DATA"},
    new clsSort{ AccountType = "ND|10|17", Description = "ND DATA"},
    new clsSort{ AccountType = "OL|52|01", Description = "OL DATA"},
    new clsSort{ AccountType = "XY|52|01", Description = "OL DATA"},
    new clsSort{ AccountType = "ER|60|01", Description = "OL DATA"},
    new clsSort{ AccountType = "ER|41|01", Description = "ER 41 DATA "},
    new clsSort{ AccountType = "ER|01|01", Description = "ER 01 DATA"}};

    //it should sort based on account type. ND, SV, OL, ER|41 to 50, ER|01 to 25, ER|26 to 31
    //For ER account type we need to consider code also (2nd value after split by |)

    //Desired output for sort
    //AccountType = "ND|10|17", Description = "ND DATA"
    //AccountType = "SV|00|20", Description = "SV a 00 DATA"
    //AccountType = "SV|40|01", Description = "SV b 40 DATA"
    //AccountType = "OL|52|01", Description = "OL DATA"
    //AccountType = "ER|41|01", Description = "ER 41 DATA "
    //AccountType = "ER|01|01", Description = "ER 01 DATA"
    //AccountType = "ER|31|01", Description = "ER 31 DATA"

    list = list.OrderBy(a => a.Description).ToList();

    List<clsSort> result = new List<clsSort>();

    var temp = list.Where(a => a.AccountType.Substring(0, 2) == "ND").OrderBy(a=>a.Description).ToList();
    foreach (var v in temp)
    {
    result.Add(v);
    }

    temp = list.Where(a => a.AccountType.Substring(0, 2) == "SV").OrderBy(a => a.Description).ToList();
    foreach (var v in temp)
    {
    result.Add(v);
    }

    temp = list.Where(a => a.AccountType.Substring(0, 2) == "OL").OrderBy(a => a.Description).ToList();
    foreach (var v in temp)
    {
    result.Add(v);
    }

    temp = list.Where(a => a.AccountType.Substring(0, 2) == "ER" && (Convert.ToInt32(a.AccountType.Substring(3, 2)) >= 41
    && Convert.ToInt32(a.AccountType.Substring(3, 2)) <= 50)).OrderBy(a => a.Description).ToList();
    foreach (var v in temp)
    {
    result.Add(v);
    }

    temp = list.Where(a => a.AccountType.Substring(0, 2) == "ER" && (Convert.ToInt32(a.AccountType.Substring(3, 2)) >= 1
    && Convert.ToInt32(a.AccountType.Substring(3, 2)) <= 25)).OrderBy(a => a.Description).ToList();
    foreach (var v in temp)
    {
    result.Add(v);
    }

    temp = list.Where(a => a.AccountType.Substring(0, 2) == "ER" && (Convert.ToInt32(a.AccountType.Substring(3, 2)) >= 26
    && Convert.ToInt32(a.AccountType.Substring(3, 2)) <= 31)).OrderBy(a => a.Description).ToList();
    foreach (var v in temp)
    {
    result.Add(v);
    }

    var finalResult = result.Union(list).ToList();

    }

    }

    class clsSort
    {
    public string Description { get; set; }
    public string AccountType { get; set; }
    }
    }
  • #748842
    1 more update:

    With the current code if there is any data,

    Ex : new clsSort{ AccountType = "AB|00|20", Description = "AB a 00 DATA"}

    This will not add to result collection.
    But I need this data also ( at the end of the collection also fine).

    Happy Coding.....
    Naveen V S

  • #748884
    Guys I am waiting for answer, plz help me.
    Happy Coding.....
    Naveen V S

  • #748996
    Hi,
    I will look into this by Monday. good practice to do also.

  • #752566
    Hi,

    XY is missing in your desired output, Can you provide me the desired output

  • #752572
    Hi,

    Try this,

    foreach (var item in list)
    {
    bool r = result.Where(a => a.AccountType == item.AccountType).Any();
    if (!r)
    {
    result.Add(item);
    }
    }


  • Sign In to post your comments