【问题标题】:LINQ WHERE DATE Issue [duplicate]LINQ WHERE DATE 问题 [重复]
【发布时间】:2013-08-17 09:19:26
【问题描述】:

有人指出问题吗? 不断收到“LINQ to Entities 不支持指定的类型成员 'Date'。仅支持初始化程序、实体成员和实体导航属性。”

public IEnumerable<Appointment> FindAllAppointmentsWithReminders()
{
    DateTime reminderDate = DateTime.Today.Date;
    IEnumerable<Appointment> apps = RepositorySet    
        .OfType<Appointment>()
        .Include("Client")
        .Where(c => EntityFunctions.TruncateTime(c.Client.Reminder.Date) == reminderDate.Date 
                        && reminderDate.Date > EntityFunctions.TruncateTime(c.StartTime.Date));

    return apps;
}                         

【问题讨论】:

  • 你在哪一行得到错误?

标签: c# linq entity-framework


【解决方案1】:

试试这个

public IEnumerable<Appointment> FindAllAppointmentsWithReminders()
{
    DateTime reminderDate = DateTime.Today.Date;
**DateTime NewDate =reminderDate.Date;**
    IEnumerable<Appointment> apps = RepositorySet    
        .OfType<Appointment>()
        .Include("Client")
        .Where(c => EntityFunctions.TruncateTime(c.Client.Reminder.Date) == **NewDate** 
                        && reminderDate.Date > EntityFunctions.TruncateTime(c.StartTime.Date));

    return apps;
}          

【讨论】:

    【解决方案2】:

    从您的方法中删除所有.Date,但是:

    DateTime reminderDate = DateTime.Today.Date;
    

    EntityFramework 不支持Datetime.Date 属性。出于这个原因,有伪函数EntityFunctions.TruncateTime,对于reminderDate,你已经删除了DateTime reminderDate = DateTime.Today.Date中的时间。

    public IEnumerable<Appointment> FindAllAppointmentsWithReminders()
    {
        DateTime reminderDate = DateTime.Today.Date;
        IEnumerable<Appointment> apps = RepositorySet    
            .OfType<Appointment>()
            .Include("Client")
            .Where(c => EntityFunctions.TruncateTime(c.Client.Reminder) == reminderDate 
                            && reminderDate > EntityFunctions.TruncateTime(c.StartTime));
    
        return apps;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多