To compare Date and Datetime in ASP.NET mvc 3?

本文关键字:NET mvc ASP in compare Date and Datetime To | 更新日期: 2023-09-27 18:21:25

我是ASP.NET新手。会议表中有date_meeting(Datetime)列。我想从表中加载数据,即日期会议的日期是今天。我尝试了任何变体:

List<Meeting> meetings = db.Meetings.Where(x => x.date_meeting == Convert.ToDateTime(DateTime.Now.ToString("dd.mm.yyyy")).Date && x.langId == lang).ToList();

错误:

LINQ to Entities无法识别方法"System.DateTime"ToDateTime(System.String)"方法,而此方法不能翻译成商店表达式。

我需要这个逻辑。

List<Meeting> meetings = db.Meetings.Where(x => x.date_meeting.ToString("dd.mm.yyyy") == DateTime.Now.ToString("dd.mm.yyyy") && x.langId == lang).ToList();

To compare Date and Datetime in ASP.NET mvc 3?

或者您可以使用

List<Meeting> meetings = db.Meetings.Where( m.date_meeting.Day.Equals(DateTime.Now.Day) &&
                                     m.date_meeting.Month.Equals(DateTime.Now.Month) &&
                                     m.date_meeting.Year.Equals(DateTime.Now.Year) && && x.langId == lang).ToList();

或者试试这篇文章:LINQ查询只比较DateTime 的日期部分

尝试

List<Meeting> meetings = db.Meetings.Where(x => x.date_meeting.Date == DateTime.Now.Date && x.langId == lang).ToList();