开发者

LINQ group by and compare date

开发者 https://www.devze.com 2023-02-06 20:51 出处:网络
I have the following: var currentDate = DateTime.UtcNow; var calendarEntry = from item in new CalendarEntryRepository(this.Db).List().Where(x => x.Culture == language.Value)

I have the following:

    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
                  };

From CalendarEntryRepository I want to group by ContentObjectId (this works fine). However when i add this line:

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

I keep getting this error:

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

What i'm trying to do is group by ContentObjectId and then get StartDate that is greater than today.

I'm using entity framwork and MS SQL2008 thanks


The Min Value of DateTime in your database has a lower value that the MinValue defined int he culture you used in your App.


Change the StartDate column in DataBase to data type DateTime2 to support a broader range of dates

0

精彩评论

暂无评论...
验证码 换一张
取 消