This question already has answers here:
The specified type member 'Date' is not supported in LINQ to Entities Exception

(9 个回答)


7年前关闭。




有人指出问题吗?
不断收到“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;
}

最佳答案

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

DateTime reminderDate = DateTime.Today.Date;

EntityFramework 不支持 .DateDatetime 属性。出于这个原因,有伪函数 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;
}

关于c# - LINQ WHERE 日期问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18248384/

10-12 12:37
查看更多