HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

LINQ Orderby Descending Query

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
orderbylinqdescendingquery

Problem

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?

Code Snippets

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

Context

Stack Overflow Q#5344805, score: 779

Revisions (0)

No revisions yet.