检查日期是否为此日期或更大

6 c# linq datetime

我试图用布尔值检查我的linq查询中的日期是这个日期还是更大.但它不像我想要的那样工作.

这是我的代码

        public bool CheckMonth(int month)
    {
            if (month > System.DateTime.Now.Month)
            {
                return true;
            }
            else if (month == System.DateTime.Now.Month)
            {
                return true;
            }
            else
            {
                return false;
            }
    }

    public virtual IList<DateItem> GetThreeDateToList()
    {
        var data = new ScoutDataDataContext();

        var q = (from d in data.DateDetails
                 where d.Activate == 1 && CheckMonth(d.EndDate.Month) 
                 orderby d.Date.Date.Month descending
                 select new DateItem
                 {
                     Title = d.Title,
                     Date = d.Date.Date + " - " + d.EndDate.Date,
                     Link = d.Link,
                 }).Take(3);

        return q.ToList();
    }
Run Code Online (Sandbox Code Playgroud)

谁有不同的方式?

Fre*_*örk 26

你想做什么?根据您的文本,您想知道给定日期是今天还是更晚,但代码示例仅比较月份(这意味着今年6月与去年6月相同).如果您想比较日期(包括年和日),这个比较将为您完成工作:

yourDate.Date >= DateTime.Now.Date
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用DateTime.Today提高可读性 (2认同)