none
Order by not working?!? RRS feed

  • Question

  • I have this following Entity Framework query:

    orders = (From c In ef.Customers
              From o In c.Orders
              From i In o.Items 
              Where (c.CustomerName = customerName) AndAlso (i.ItemName = itemName)
              Order By o.OrderNumber Select o}).Distinct.ToList()
    The query works, but the weird thing is that the orders list being returned is not ordered.

    Does anyone have an explantion as to why that is, or any suggestions of how to resolve the problem?

    Richard

    Wednesday, February 8, 2012 7:18 AM

Answers

  • Hi RichardR,

    Welcome!

    I think you can try to put "Order By" after "Select o"(... Select o).OrderBy(t=>t.OrderNumber).

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by RichardR Wednesday, February 8, 2012 10:37 PM
    Wednesday, February 8, 2012 8:58 AM
    Moderator

All replies

  • Hi RichardR,

    Welcome!

    I think you can try to put "Order By" after "Select o"(... Select o).OrderBy(t=>t.OrderNumber).

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by RichardR Wednesday, February 8, 2012 10:37 PM
    Wednesday, February 8, 2012 8:58 AM
    Moderator
  • Hi,

    The key point to keep in mind is that distinct is done by sorting and keeping unique rows so you just sort again the rows and your first order by may appear to have no effect at all. So use distinct first and then only add your order by clause...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, February 8, 2012 9:31 AM
  • Thanks.  That apparently did it.

    Actually, for anyone else doing VB, the query statement ended up as:

    orders = (From c In ef.Customers
              From o In c.Orders
              From i In o.Items 
              Where (c.CustomerName = customerName) AndAlso (i.ItemName = itemName)
              Select o}).Distinct.OrderBy(Function(o) o.OrderNumber).ToList()

    Wednesday, February 8, 2012 10:42 PM