Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

c# - LINQ Orderby降序查询(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.

(我有一个LINQ查询,我想按最近创??建的日期排序。)

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

(方法'OrderByDescending'的重载不带0参数)

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

(谢谢 :))

  ask by 109221793 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to choose a Property to sort by and pass it as a lambda expression to OrderByDescending

(您需要选择要排序的属性并将其作为lambda表达式传递给OrderByDescending)

like:

(喜欢:)

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

Really, though the first version of your LINQ statement should work.

(真的,虽然你的LINQ语句的第一个版本应该工作。)

Is t.Delivery.SubmissionDate actually populated with valid dates?

(t.Delivery.SubmissionDate实际上是否填充了有效日期?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...