Why won't this sort RRS feed

  • Question

  • Hi guys

    I've got a column in my table called publicationYearShort of type Int.  For some reason, running the following statement doesn't order by this column:

    (from a in Articles _
    Order By(a.PublicationYearShort) ascending
    select a.PublicationYearShort).Distinct().ToList

    What am I doing wrong?



    Monday, March 25, 2013 7:35 PM


  • Hi Steven;

    That is because the Distinct method call does not guaranteed to keep the results in the same order. So you need to call Distinct before calling OrderBy. Like in the following.

    (from a in Articles _
     select a.PublicationYearShort).Distinct()
     .OrderBy( p => p).ToList();

    That will give the list in ascending order.



    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by steven_455 Tuesday, March 26, 2013 12:35 PM
    Monday, March 25, 2013 8:31 PM