简体   繁体   English

要在ASP.NET MVC 3中比较日期和日期时间?

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

I'm new in ASP.NET. 我是ASP.NET的新手。 I have date_meeting (Datetime) column in the meetings table. 我在会议表中有date_meeting(Datetime)列。 I want to load data from table, which are the date_meeting's date is today. 我想从表中加载数据,即date_meeting的日期为今天。 I tried any variations: 我尝试了任何变体:

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

error: 错误:

LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression. LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)',并且该方法无法转换为商店表达式。

I need this logic. 我需要这种逻辑。

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

or you can use 或者你可以使用

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();

Or try this article: LINQ query to compare only date part of DateTime 或尝试使用本文: LINQ查询以仅比较DateTime的日期部分

尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM