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 :)
If you want to rearrange or sort the elements of the given sequence or collection in descending order in query syntax, then use descending keyword as shown in below example. And in method syntax, use OrderByDescending () method to sort the elements of the given sequence or collection.
LINQ includes five sorting operators: OrderBy, OrderByDescending, ThenBy, ThenByDescending and Reverse. LINQ query syntax does not support OrderByDescending, ThenBy, ThenByDescending and Reverse. It only supports 'Order By' clause with 'ascending' and 'descending' sorting direction.
OrderByDescending<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) Sorts the elements of a sequence in descending order according to a key.
In a query expression, the orderby clause causes the returned sequence or subsequence (group) to be sorted in either ascending or descending order. Multiple keys can be specified in order to perform one or more secondary sort operations. The sorting is performed by the default comparer for the type of the element.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With