简体   繁体   English

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'

[英]LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)'

In LINQ I want make thw follow query: 在LINQ中我想要跟随查询:

_tareaRepositorio.ObtenerTodo()
  .Where(
   x =>
     Convert.ToDateTime(x.FechaCreacion.ToString("d")) >= fechaInicial &&
     Convert.ToDateTime(x.FechaCreacion.ToString("d")) <= fechaFinal).ToList();

But, at the moment of execute the query appears the follow mistake: 但是,在执行时,查询出现以下错误:

LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression. LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法,并且此方法无法转换为商店表达式。

How I can fix that ? 我怎么解决这个问题?

EDIT 编辑

The trouble is that I need compare just date, not time ... 麻烦的是我需要比较日期,而不是时间......

Try this 试试这个

EDIT: Add DbFunctions.TruncateTime() to get the desired effect 编辑:添加DbFunctions.TruncateTime()以获得所需的效果

_tareaRepositorio.ObtenerTodo()
    .Where( x => DbFunctions.TruncateTime(x.FechaCreacion) >= fechaInicial &&
                 DbFunctions.TruncateTime(x.FechaCreacion) <= fechaFinal)
    .ToList();

The exception you are getting is because the Convert.ToDateTime is a .NET method that cannot be converted into SQL. 您获得的异常是因为Convert.ToDateTime是一个无法转换为SQL的.NET方法。 Normally this should be done after the query has been materialized (ie using .ToList() before the Where ) but in your particular case it is unnecesary since DateTime objects can be compared using >= and <= and Entity Framework can convert it to SQL successfully 通常这应该在查询实现之后完成(即在Where之前使用.ToList() )但在特定情况下它是不必要的,因为可以使用>=<=来比较DateTime对象,并且Entity Framework可以将它转换为SQL顺利

Now if you only want to compare the Date part of the Datetime you can use the method DbFunctions.TruncateTime() which lives inside System.Data.Entity namespace. 现在,如果您只想比较Datetime的Date部分,可以使用位于System.Data.Entity命名空间内的方法DbFunctions.TruncateTime() This method will allow EF to correctly truncate the date in SQL 此方法将允许EF在SQL中正确截断日期

If you want to get entities between two dates, compare as dates: 如果要在两个日期之间获取实体,请将其作为日期进行比较:

var result = content.Table.Where(x => x.date >= startDate
                                 && x.date <= endDate)
                           .ToList();

where startDate and endDate are both DateTime . 其中startDateendDate都是DateTime

Converting to strings means you are at the mercy of the current culture (locale) settings. 转换为字符串意味着您受当前文化(区域设置)设置的支配。 With the invariant culture the default date format is American, so they do not compare lexicographically with any useful sense. 对于不变文化,默认日期格式是美式,因此它们不会在字典上与任何有用的意义进行比较。

Return your result with AsEnumerable() then convert it to list. 使用AsEnumerable()返回结果,然后将其转换为列表。

It will work i am facing the same problem as you this solution might be work. 它将工作我面临同样的问题,因为这个解决方案可能是有效的。

使用DbFunctions.CreateDateTime(2019,1,28,0,0,0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法&#39;System.DateTime ToDateTime(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method LINQ to Entities无法识别方法&#39;System.DateTime ToDateTime(System.String)&#39; - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' ASP.NET实体框架LINQ to Entities无法识别方法&#39;System.DateTime ToDateTime(System.String) - ASP.NET Entity Framework LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String) LINQ to Entities无法识别方法ToDateTime(System.String) - LINQ to Entities does not recognize the method ToDateTime(System.String) LINQ to Entities无法识别方法&#39;System.DateTime ConvertShamsiToMiladi(System.String)&#39;方法 - LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method LINQ to Entities无法识别MVC5中的方法&#39;System.DateTime ParseExact(System.String,System.String,System.IFormatProvider)&#39;方法错误 - LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method error in MVC5 LINQ to Entities 无法识别方法“System.DateTime GetValueOrDefault()” - LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()' LINQ to Entities 无法识别“System.String Decrypt(System.String, System.String)”方法 - LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method LINQ to Entities无法识别方法&#39;System.String getFullname(System.String,System.String)&#39; - LINQ to Entities does not recognize the method 'System.String getFullname(System.String, System.String)' LINQ to Entities无法识别方法&#39;System.String ToString()&#39;方法++++++ - LINQ to Entities does not recognize the method 'System.String ToString()' method ++++++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM