LINQ Orderby Descending Query

109221793 picture 109221793 · Mar 17, 2011 · Viewed 931.1k times · Source

I'm sure this will be a relatively simple one.

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

See:

        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, I'm fairly sure the first way I've done it should work. I've tried changing descending to ascending just to see if it does anything but it stays the same.

I'd be grateful if someone could take a look at the query and see if I'm doing anything wrong. Thanks :)

Answer

Adam Price picture Adam Price · Mar 17, 2011

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?