【问题标题】:The specified type member 'Date' is not supported in LINQ to Entities [duplicate]LINQ to Entities 不支持指定的类型成员“日期”[重复]
【发布时间】:2013-12-22 07:18:06
【问题描述】:

我的 asp.net mvc web 应用程序中有以下方法:-

public SystemInformation GetSystemInfo(int pagesize)           
{
    var d = DateTime.Today;
    SystemInformation s = new SystemInformation()
    {                            
       DeleteNo = tms.TechnologyAudits.Where(a => (EntityFunctions.TruncateTime(a.DateTimeEnd.Date) == EntityFunctions.TruncateTime(d) && a.AuditAction.Name.ToLower() == "delete")).Count(),

    };
    return s;
}

但上述情况引发了以下异常:-

LINQ to 不支持指定的类型成员“日期” 实体。只有初始化器、实体成员和实体导航 支持属性。

【问题讨论】:

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

无法翻译的是a.DateTimeEnd.Date。等效为 EntityFunctions.TruncateTime(a.DateTimeEnd) 。您不需要/不能同时使用两者。

DateTime.Date

与此实例具有相同日期和时间值的新对象 设置为午夜 12:00:00 (00:00:00)

EntityFunctions.TruncateTime

清除时间部分的输入日期。

【讨论】:

  • 非常感谢,我明白了这个问题..
【解决方案2】:

LINQ to Entities 无法将大多数 .NET Date 方法(包括您使用的转换)转换为 SQL,因为没有等效的 SQL。

在你的情况下 LINQ 不能翻译这个

(EntityFunctions.TruncateTime(a.DateTimeEnd.Date)

解决方法是在LINQ语句之外强制转换对象,然后传入一个值。

【讨论】:

  • 但是在我的情况下我怎么能做到这一点,因为我在 where 中使用日期,所以我不能将对象投射到我的 where () 之外?你能建议吗?
猜你喜欢
  • 2020-05-30
  • 2015-01-27
  • 2012-07-17
  • 2012-07-20
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
相关资源
最近更新 更多