LINQ Orderby Descending Query

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 :)


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?


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;

我认为第二个应该是

var itemList = (from t in ctn.Items
                where !t.Items && t.DeliverySelection
                select t).OrderByDescending(c => c.Delivery.SubmissionDate);
链接地址: http://www.djcxy.com/p/34236.html

上一篇: %P在gcc内联汇编中代表什么?

下一篇: LINQ Orderby降序查询