Linq到实体日期时间错误 [英] Error Linq to Entities Datetime
问题描述
我有以下代码
var dates = query.Select(
x => DateTime.ParseExact(x.Date, "yyyy-MM", CultureInfo.InvariantCulture));
var minDate = dates.Min(x => x);
但是当我执行它,我得到例外
But When I execute that, I get the exception
System.Data.Entity.dll,但未在用户代码中处理
System.Data.Entity.dll but was not handled in user code
附加信息:LINQ to Entities不识别方法
'System.DateTime ParseExact(System.String,System.String,
System.IFormatProvider)'方法,并且此方法无法将
转换为存储表达式。
Additional information: LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method, and this method cannot be translated into a store expression.
我做错了什么?而且我该如何解决呢?
What am I doing wrong? And how I can fix that?
推荐答案
那个错误其实很清楚。 Linq到ParseExact到SQL的实体没有翻译。
Well, the error is actually quite clear. There is no translation in Linq to Entities of ParseExact to SQL.
记住,根据封面,实体框架将查询转换为SQL命令或一组命令。如果EF不知道如何翻译某些东西,则会引发此错误。
Remember, Entity Framework, under the covers, converts the query to a SQL command or set of commands. If EF doesn't know how to translate something, it throws this error.
一个可能的解决方案虽然不是非常有效,但是将IQueryable转换为IEnumerable,允许您执行该语句。
One possible solution, while not terribly efficient, is to convert the IQueryable to IEnumerable, which will allow you to execute the statement.
var dates = query.ToList().Select(
x => DateTime.ParseExact(x.Date, "yyyy-MM", CultureInfo.InvariantCulture));
这篇关于Linq到实体日期时间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!