EF 4 in C#.
I have my query, at the moment I'm able to filter the result by the current Date (just date not considering TIME).
I need to filter the result FROM the last two days TO the current Date (no idea how to do it).
I tried in my query currenteDate - 2
but without success.
Could you please give me an example? Thanks for your time on this开发者_如何学Python.
DateTime currenteDate = DateTime.UtcNow.Date;
var contents = from cnt in context.CmsContents
where cnt.IsPublished == true & cnt.IsDeleted == false & cnt.Date == currenteDate
orderby cnt.ContentId descending
select new { cnt.ContentId, cnt.Title, cnt.TitleUrl, cnt.Date, cnt.TypeContent };
For changing current date you need use currenteDate.AddDays(-2)
. And use >=
instead of ==
to get all records from 2 days before and till the last record
DateTime currenteDate = DateTime.UtcNow.Date.AddDays(-2);
var contents = from cnt in context.CmsContents
where cnt.IsPublished == true & cnt.IsDeleted == false & cnt.Date >= currenteDate
orderby cnt.ContentId descending
select new { cnt.ContentId, cnt.Title, cnt.TitleUrl, cnt.Date, cnt.TypeContent };
use the compare method from the DateTime-object:
cnt.Date.CompareTo( DateTime.Now.AddDays( -2 ) ) >= 0
精彩评论