tags:

views:

74

answers:

1

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

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!

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)

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...

Steve

EDITED

Ok some more information for you all

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

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

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

While Gives

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

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

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

+1  A: 

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)

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

alt text

p.campbell