none
c# linq orderby orders incorrectly or not in number seqence RRS feed

  • Question

  • I am trying to sort numbers as a string but output is incorrect. Below is my query

    var wdtest = admintable.Queryable() 
                                .Include(x=> x.emp.Select(t=> t.empId))
                                .Include(x => x.Team.Select(wt => w.departments))
                                .Where(b => b.baseId == Id);

    wdtest  = wdtest .Distinct().OrderBy(f => f.EmpNumber.Length).ThenBy(f => f.EmpNumber);

    I want an output of 

    2000-1

    2000-2

    A4455

    B564

    C78788848-88

    D23

    But I am currently getting the output below. Any help will be appreciated

    2000-1

    2000-2

    D23

    B564

    A4455

    C78788848-88


    Ebenezer

    Friday, December 14, 2018 3:07 AM

Answers

  • Hi denkyira,

    Please try the following code.

    var result = wdtest.Distinct().OrderBy(f => f.EmpNumber.Substring(0,1)).ThenBy(f => f.EmpNumber.Length).ToList();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by denkyira Monday, December 17, 2018 5:47 PM
    Monday, December 17, 2018 2:24 AM
    Moderator

All replies

  • Hi Denkyira,

    Please modify the code like code and check if it works for you.

    var wdtest = admintable.Queryable() 
                                .Include(x=> x.emp.Select(t=> t.empId))
                                .Include(x => x.Team.Select(wt => w.departments))
                                .Where(b => b.baseId == Id);
    
    wdtest  = wdtest .Distinct().OrderBy(f => f.EmpNumber).ThenBy(f => f.EmpNumber.Length);

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 14, 2018 6:04 AM
    Moderator
  • Hi Zhanglong

    Thanks for your response. I have made the changes you suggested but the output is not write. I now get 

    2000-1

    2000-10

    2000-14

    2000-2

    A4455

    B564

    C78788848-88

    D23

    instead of 

    2000-1

    2000-2

    2000-10

    2000-14

    A4455

    B564

    C78788848-88

    D23


    Ebenezer

    Friday, December 14, 2018 12:06 PM
  • Hi denkyira,

    Please try the following code.

    var result = wdtest.Distinct().OrderBy(f => f.EmpNumber.Substring(0,1)).ThenBy(f => f.EmpNumber.Length).ToList();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by denkyira Monday, December 17, 2018 5:47 PM
    Monday, December 17, 2018 2:24 AM
    Moderator
  • Hi Zhanglong

    Thanks a lot for your response. Although it didn't work, they have accepted my first solution since they are only entering numbers. Thanks for your help


    Ebenezer

    Monday, December 17, 2018 5:47 PM