【问题标题】:Using DbFunctions.TruncateDate and still have error: **The specified type member 'Date' is not supported in LINQ to Entities.**使用 DbFunctions.TruncateDate 并且仍然有错误:**LINQ to Entities 不支持指定的类型成员“日期”。**
【发布时间】:2018-04-17 08:05:56
【问题描述】:

我收到一个错误: LINQ to Entities 不支持指定的类型成员“日期”。

我有一个代码,我试图遵循其他两个帖子的答案:LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' methodLinq To Sql compare Time only

这个方法也是由许多线程并行执行的。

public List<Element> GetResults()
{
    List<Element> elements = new List<Element>();

   try {

    using (DbContext context = new EntitiesContext())
    {

    DateTime dateTimeNow = DateTime.Now;
    TimeSpan timeSpanNow = DateTime.Now.TimeSpan;      
    elements = context.Elements.
                          // Some other Includes
                          //Element has a field System.DateTime DateTime
                          .Include(_ => _.DateTime)              
                          .Where(o => DbFunctions.TruncateDate(o.DateTime.Date) == dateTimeNow 
&& DbFunctions.CreateTime(o.DateTime.Hour, o.DateTime.Minute, o.DateTime.Second) <= timeSpanNow))
.ToList();


    }
}
catch(Exception ex)
{
    //logging
} 

return elements;
    }

【问题讨论】:

  • DbFunctions.TruncateDate(o.DateTime.Date) 应该是 DbFunctions.TruncateTime(o.DateTime)
  • @Evk 我会试试的,非常感谢。
  • 好的@Evk,工作,发布答案,我会接受它。

标签: c# .net entity-framework linq entity-framework-6


【解决方案1】:

首先,据我所知,没有DbFunctions.TruncateDate 这样的东西。我想这只是一个错字,你的意思是DbFuctions.TruncateTime

然后,您使用的是DbFunctions.TruncateTime,因为 EF 不知道如何将 someDateTime.Date 转换为 SQL。通过做

DbFunctions.TruncateDate(o.DateTime.Date)

你仍在使用 EF 不理解的东西,所以你需要摆脱那个 Date:

DbFunctions.TruncateDate(o.DateTime)

【讨论】:

    猜你喜欢
    • 2020-05-30
    • 2013-12-22
    • 2015-01-27
    • 2012-07-17
    • 2012-07-20
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多