简体   繁体   中英

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

I'm new in ASP.NET. I have date_meeting (Datetime) column in the meetings table. I want to load data from table, which are the date_meeting's date is today. 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.

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

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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