2014-09-10 143 views
0

我是ASP.NET新手。我在會議表中有date_meeting(日期時間)列。我想從表中加載數據,這是date_meeting的日期是今天。 我嘗試的任何變化:在ASP.NET mvc 3中比較日期和日期時間?

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 does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression.

我需要這種邏輯。

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

回答

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 query to compare only date part of DateTime

+0

錯誤:底層提供程序在打開時失敗。 – 2014-09-10 12:34:59

+1

http://www.codeproject.com/Articles/499987/LINQ-query-to-compare-only-date-part-of-DateTime試試這個 – 2014-09-10 12:39:12

+0

成功:'列表 meeting = db.Meetings.Where(x = > x.date_meeting.Value.Day == DateTime.Now.Day && x.date_meeting.Value.Month == DateTime.Now.Month && x.date_meeting.Value.Year == DateTime.Now.Year && x。 langId == lang).ToList();'感謝鏈接@meajaygoel – 2014-09-10 12:55:32

1

嘗試

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

System.Nullable 」不包含定義「Date」並未找到擴展方法「Date」,接收類型爲「System.Nullable 」作爲第一個證明t(可能錯過了使用指令或程序集引用) – 2014-09-10 12:05:37

+1

@MehmanBashirov - 嘗試'x.date_meeting.Value.Date' – 2014-09-10 12:06:33

+0

x.date_meeting.Value.Date == DateTime.Now.Date'指定的類型成員'Date'不是在LINQ to Entities中受支持。僅支持初始化程序,實體成員和實體導航屬性。 – 2014-09-10 12:15:07