Hello, I'm having some difficulties to sort my results by Date. Is there any special method? Because I'm doing this right now:
var db = new DB();
var articles = db.Articles;
var orderedArticles = articles.OrderBy(a => a.Date);
return View(orderedArticles.ToList());
Where Date is a datetime field. And there is no effect for OrderBy(..) or OrderByDescending(..)
So I managed to check what is happening.
Everytime I add a new Article I'm just using the date on not the time so if I have two articles both for the same day for example: with:
var orderedArticles = db.Articles.OrderByDescending(a => a.Date).ToList();
I would have
Id Title Date
10 First Added Article 16/09/2009 00:00
11 Second Added Article 16/09/2009 00:00
15 Old Article Added Later 15/09/2009 00:00
So you can see that is filtering by date, but the thing is when I have the same date the sorting loses the focus. So what I did is, orderBy two different contexts like first order by Id and later order by Date:
var orderedArticles = db.Articles.OrderByDescending(a => a.Id).OrderByDescending(a => a.Date).ToList();
So after this I have the following:
Id Title Date
11 Second Added Article 16/09/2009 00:00
10 First Added Article 16/09/2009 00:00
15 Old Article Added Later 15/09/2009 00:00
I really don't know if this is the right way to do it because the main problem is that when you submit a date field like 16/09/2009 it sets the time to 00:00 and this is a problem on this situation.