簡體   English   中英

如何編寫 LINQ 查詢以獲取過去 7 天內的內容?

[英]How Can I Write a LINQ Query to Get Content Within the Last Seven Days?

我有這個 LINQ 查詢:

Contents.Where(Content => Content.Categories.Contains("top story") And Content.RunDate.Value <= DateTime.Now)
.Distinct() 
.OrderByDescending(Content => Content.RunDate)
.Take(4)

我需要修改它,以便它只抓取不超過 7 天的內容。 知道我怎么做嗎?

使用DateTime.Now.AddDays(count)其中count甚至可以是負值

Contents.Where(content => content.RunDate.Value >= DateTime.Now.AddDays(-7) &&  
                          content.RunDate.Value <= DateTime.Now)

暫無
暫無

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

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