ChatGPT解决这个技术问题 Extra ChatGPT

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?


B
Bakudan

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?


Hi optus, thanks for your reply. I've figured out what the problem was. I was using a paginatedList, and it was actually doing the ordering from that helper class. I'll mark your answer as correct once the time limit is up :)
m
mrosiak

I think this first failed because you are ordering value which is null. If Delivery is a foreign key associated table then you should include this table first, example below:

var itemList = from t in ctn.Items.Include(x=>x.Delivery)
                    where !t.Items && t.DeliverySelection
                    orderby t.Delivery.SubmissionDate descending
                    select t;

include is equivalent to DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith(i => i.Delivery); ctn.LoadOptions = dlo;
a
abatishchev

I think the second one should be

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

I think to be fair to him, I had typed the query wrong when I posted (ie. Leaving in the second last orderby line) and he probably just copied and pasted to demonstrate the use of the lambda OrderByDescending.
B
Bernard Vander Beken

Just to show it in a different format that I prefer to use for some reason: The first way returns your itemList as an System.Linq.IOrderedQueryable

using(var context = new ItemEntities())
{
    var itemList = context.Items.Where(x => !x.Items && x.DeliverySelection)
                                .OrderByDescending(x => x.Delivery.SubmissionDate);
}

That approach is fine, but if you wanted it straight into a List Object:

var itemList = context.Items.Where(x => !x.Items && x.DeliverySelection)
                                .OrderByDescending(x => x.Delivery.SubmissionDate).ToList();

All you have to do is append a .ToList() call to the end of the Query.

Something to note, off the top of my head I can't recall if the !(not) expression is acceptable in the Where() call.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now