Search code examples
c#linqsql-order-by

LINQ Orderby Descending Query


I have a LINQ query that I want to order by the most recently created date.

I tried:

var itemList = from t in ctn.Items
                    where !t.Items && t.DeliverySelection
                    orderby t.Delivery.SubmissionDate descending
                    select t;

I have also tried:

var itemList = (from t in ctn.Items
                     where !t.Items && t.DeliverySelection
                     select t).OrderByDescending();

but this gives an error :

No overload for method 'OrderByDescending' takes 0 arguments

From what I've read, the first way it should work. I've tried changing descending to ascending just to see if it does anything but it stays the same.

What am I doing wrong?


Solution

  • You need to choose a Property to sort by and pass it as a lambda expression to OrderByDescending

    like:

    .OrderByDescending(x => x.Delivery.SubmissionDate);
    

    Really, though the first version of your LINQ statement should work. Is t.Delivery.SubmissionDate actually populated with valid dates?