簡體   English   中英

如何使用EF 4獲取當前日期過去兩天內的所有記錄?

[英]How to get all records within the last Two Days from the current Date using EF 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 };

要更改當前日期,您需要使用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 };

使用DateTime-object中的compare方法:

cnt.Date.CompareTo( DateTime.Now.AddDays( -2 ) ) >= 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM