开发者

How to get all records within the last Two Days from the current Date using EF 4?

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜