2010-10-19 135 views
2

比较我有这样的代码实体框架和LINQ - 日期时间

public List<CalendarData> GetCalendarData(DateTime day) 
    { 
     List<CalendarData> list = new List<CalendarData>(); 
     using (dataContext = new VTCEntities()) 
     { 

      DateTime test = new DateTime(2010, 10, 20, 17, 45, 0); 

      var data = from z in dataContext.ReservationsSet 
         where z.start_time.Value == test 
         select z; 

      foreach (var r in data) 

我希望做的是有这个

var data = from z in dataContext.ReservationsSet 
        where z.start_time.Value == day 
        select z; 

我的问题是,z.start_time有时间部分也是。 DateTime日没有记录时间部分。有没有办法比较的日期部分没有收到此错误

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. 

当我做这个

var data = from z in dataContext.ReservationsSet 
      where z.start_time.Value.Date == test 
      select z; 

回答

7

一种选择是计算两个值,就像这样:

DateTime day = ...; 
DateTime nextDay = day.AddDays(1); 

var data = from z in dataContext.ReservationsSet 
        where z.start_time.Value >= day && 
         z.start_time.Value < nextDay 
        select z; 
+0

乔恩,谢谢,如何翻转简单。我不知道为什么我比他们更难做事。你是男人。 – jim 2010-10-19 17:04:28

2

您不能在实体框架中使用.Date。我知道处理这个最简单的方法是使最低+最高日:

DateTime test = new DateTime(2010, 10, 20, 17, 45, 0); 
DateTime startDay = test.Date; 
DateTime endDay = startDay.AddDays(1); 

var data = from z in dataContext.ReservationsSet 
        where z.start_time.Value >= startDay && z.start_time.Value < endDay 
        select z; 
+0

非常感谢您的帮助。 – jim 2010-10-19 23:25:13