Linq OrderByDescending ThenByDescending问题 [英] Linq OrderByDescending ThenByDescending Issue

查看:324
本文介绍了Linq OrderByDescending ThenByDescending问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对linq订购有疑问. 我有一个带有两个日期时间字段的对象.一个只会存储日期(例如2010年8月2日),而将时间保留为全0

Hi im having a problem with ordering with linq. I have a object with two datetime fields. One just stores the date (e.g. 8/2/2010) and leaves the time as all 0's

在创建记录时会创建另一个datetime字段,同时还有时间.这两个字段的想法是因为我可能要在网站上添加几天前的标题/文章".因此,我选择的第一个Datetime是"Date",通常默认为今天的日期.

While the other datetime field is created when the record is made and has the time on it as well. The idea of these two field are because I may want to add a "Headline/article" that is a couple days old to my site. So the first Datetime called "Date" is the one I pick, Generally this is defaulted to todays date.

我遇到的问题是我可以按日期排序,但是如果我在同一日期添加了许多文章(因此,在日期库中,它们的日期字段都相同),则最新文章将放在这三个文章的底部!

Problem I have is I can order by The date but if I have a number of articles added on the same date (so in the datebase their date field are all identical) the newest article is put to the bottom of the three!

示例:
Article1-12/08/2010-12/08/2010 2:45 pm
Article2-12/08/2010-12/08/2010 3:45 pm
第3条-2010年12月8日-2010年12月8日下午4:45
Article4-11/08/2010-12/08/2010 2:50 pm

Example:
Article1 - 12/08/2010 - 12/08/2010 2:45pm
Article2 - 12/08/2010 - 12/08/2010 3:45pm
Article3 - 12/08/2010 - 12/08/2010 4:45pm
Article4 - 11/08/2010 - 12/08/2010 2:50pm

他们似乎只按日期字段排序,而不按日期字段排序,然后创建了日期字段,我以为我在使用命令时应该这样做(因为它们的日期字段相同)

they seem to only order by the date field and not date field then created date field which I assumed they should when I use the command (as their date fields are identical)

HeadlineRepository.All().OrderByDescending(x => x.Date).ThenByDescending(x => x.Created).Take(8)

这里有些失败,试图将它们交换,但是没有产生我想要的结果.似乎只是忽略了thenbydescending ...

Bit at a lose here tried to swap them both around but doesn't produce the result I want. seems that it just ignores the thenbydescending...

史蒂夫

已编辑

为大家提供更多信息

Headlines = repository.All().OrderByDescending(x => x.Date).ThenByDescending(x => x.Created)

产生(先显示日期,然后显示创建日期)

Produces (Date then Created displayed)

10/08/2010 00:00:00 10/08/2010 19:27:46

10/08/2010 00:00:00 10/08/2010 19:27:46

10/08/2010 00:00:00 10/08/2010 19:27:21

10/08/2010 00:00:00 10/08/2010 19:27:21

09/08/2010 00:00:00 10/08/2010 21:28:11

09/08/2010 00:00:00 10/08/2010 21:28:11

09/08/2010 00:00:00 09/08/2010 00:00:00

09/08/2010 00:00:00 09/08/2010 00:00:00

奉献时

Headlines = repository.All().OrderByDescending(x => x.Created).ThenByDescending(x => x.Date),

09/08/2010 00:00:00 10/08/2010 21:28:11

09/08/2010 00:00:00 10/08/2010 21:28:11

10/08/2010 00:00:00 10/08/2010 19:27:46

10/08/2010 00:00:00 10/08/2010 19:27:46

10/08/2010 00:00:00 10/08/2010 19:27:21

10/08/2010 00:00:00 10/08/2010 19:27:21

09/08/2010 00:00:00 09/08/2010 00:00:00

09/08/2010 00:00:00 09/08/2010 00:00:00

推荐答案

看来您的逻辑是正确的.我已经在控制台应用程序中复制了您的方案,网址为 http://pastebin.org/467317

It appears your logic is correct. I've reproduced your scenario in a console app at http://pastebin.org/467317

使用此语句将产生您期望的结果.

Using this statement produces the results you were expecting.

reg.OrderByDescending(x=>x.RegisteredOn)
   .ThenByDescending(x=>x.StartsClassOn)
   .Take(8)

您是将LINQ查询分配给变量还是确保按预期方式执行了该变量?

Are you assigning the LINQ query to a variable or ensuring somehow that it's executed as you expect?

这篇关于Linq OrderByDescending ThenByDescending问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆