2011-01-27 70 views
0

我有以下幾點:LINQ GROUP BY和比較日期

var currentDate = DateTime.UtcNow; 
    var calendarEntry = from item in new CalendarEntryRepository(this.Db).List().Where(x => x.Culture == language.Value) 
        group item by item.ContentObjectId into g 
        let itemMaxDate = g.Where(i => i.StartDate > currentDate).Select(i => i.StartDate).DefaultIfEmpty() 
        let city = g.Select(i => i.City).FirstOrDefault() 
        select new 
        { 
         ContentObjectId = g.Key, 
         StartDate = itemMaxDate, 
         City = city ?? string.Empty 
        }; 

從CalendarEntryRepository我想通過ContentObjectId組(這工作正常)。然而,當我加入這一行:

let itemMaxDate = g.Where(i => i.StartDate > currentDate).Select(i => i.StartDate).DefaultIfEmpty() 

我不斷收到此錯誤:

Message = "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value." 

我想要做的就是按ContentObjectId然後得到起始日期是大於今天。

我使用實體framwork和MS SQL2008 感謝

+0

什麼是您的數據庫? – 2011-01-27 12:06:24

+0

DateTime.Now會給你一個小於1/1/1753的日期嗎? – Magnus 2011-01-27 14:34:26

回答

1

日期時間在你的數據庫中的最小值具有較低值定義的MINVALUE詮釋,他的文化,你在你的應用程序中使用。

1

將DataBase中的StartDate列更改爲數據類型DateTime2以支持更廣泛的日期範圍