如何使用EF 4获取当前日期过去两天内的所有记录?

Gib*_*boK 4 c# linq entity-framework entity-framework-4

C#中的EF 4.

我有我的查询,目前我能够按当前日期过滤结果(只是日期不考虑TIME).

我需要将结果从过去两天过滤到当前日期(不知道怎么做).

我在查询中尝试currenteDate - 2但没有成功.

你能举个例子吗?谢谢你的时间.

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 };
Run Code Online (Sandbox Code Playgroud)

Sam*_*ich 8

要更改当前日期,您需要使用currenteDate.AddDays(-2).并且使用>=而不是==从前2天到最后一条记录获取所有记录

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 };
Run Code Online (Sandbox Code Playgroud)