Linq to sql alphanumeric sorting RRS feed

  • Question

  • Is there a way to achieve alphanumeric sorting on linq to sql, i am using c#.





    Expected result




    Saturday, August 10, 2019 5:40 PM

All replies

  • Take a look at this:

    string[] lines = { "aabbcc7", "aabbcc5", "aabbcc6" }; var orderedLines = lines.OrderBy(x => x);


    • Edited by Wizend Saturday, August 10, 2019 6:34 PM added one line
    Saturday, August 10, 2019 6:33 PM
  • Hey Thanks for the reply but its not linq to sql.

    What i was expecting is below

    Context ctx = nee Context()


    If you add atleast 10 records in the Employee table with different combitaion it wont work.

    Could you please check on this

    Sunday, August 11, 2019 4:00 AM
  • Hi harsh,

    Thank you for posting here.

    Based on your description, you want to achieve alphanumeric sorting on linq to sql.

    You could try the following code.

     DataClasses1DataContext context = new DataClasses1DataContext();
                var result = from t in context.NewSorts
                foreach (var item in result)

    Best Regards,


    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

    Monday, August 12, 2019 5:23 AM
  • This is vague description. Sorting your values is clearly how to sort it. But what about this sample?

    string[] sample = { "a1", "a2", "a10", "a20", "b1", "b10", "b2", "b20" };

    How should be sorted this sample? In default it will be in a1, a10, a2, b1, b10, b2. It can be right but it can be wrong and right order should be a1, a2, a10, b1, b2, b10. What is correct order?

    • Edited by Petr B Monday, August 12, 2019 6:27 AM
    Monday, August 12, 2019 6:26 AM
  • If all of the items start with three-letter prefix, then try this:

       var q = from row in db.MyTable

               orderby row.MyColumn.Substring( 0, 3 ), Convert.ToInt32( row.MyColumn.Substring( 3 ) )

               select row;


    In other cases, the solution needs more effort and sometimes can be done on SQL side:


    • Edited by Viorel_MVP Monday, August 12, 2019 7:52 AM
    Monday, August 12, 2019 7:43 AM