Entity Framework 4 / Linq:如何在查询中从 DateTime 转换为字符串?
我有以下查询:
from a in Products
select new ProductVM
{
id = a.id,
modified = a.modified.ToString()
}
这给了我一个错误:
LINQ to Entities does not recognize the method 'System.String ToString()'
method, and this method cannot be translated into a store expression.
Products 表中的 modified
是 DateTime。 ProductVM 类中的modified
是字符串。
有什么想法吗?这一定是一个小问题。
I have the following query:
from a in Products
select new ProductVM
{
id = a.id,
modified = a.modified.ToString()
}
Which gives me an error of:
LINQ to Entities does not recognize the method 'System.String ToString()'
method, and this method cannot be translated into a store expression.
The modified
in the Products table is DateTime.
The modified
in the ProductVM class is string.
Any ideas? This has to be a trivial issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个替代方案:
显然
DateName("MM", ..)
拼出月份名称,其中DatePart("mm", ..)
提供数值,因此StringConvert( )
,但是左边用空格填充结果,因此是.Trim()
。Here's an alternative:
Apparently
DateName("MM", ..)
spells out the month name whereDatePart("mm", ..)
provides a numeric value, thus theStringConvert( )
, but this left pads the result with spaces, thus the.Trim()
.Linq to Entities 不支持
ToString()
- 有一个函数帮助器列表作为 SqlFunctions 但这不支持日期到字符串的转换。最简单的方法是首先投影到查询中的匿名类型,然后使用
AsEnumerable()
- 之后您可以使用ToString()
,因为您现在正在使用 Linq to Objects 来处理查询表达式的其余部分 ( 此处有一篇关于此主题的长文)。ToString()
is not supported in Linq to Entities - there is a list of function helpers as part of SqlFunctions but this doesn't support Date to string conversion.Easiest would be to first project to an anonymous type within the query and then cast to an
IEnumerable
by usingAsEnumerable()
- after that you can useToString()
because you are now using Linq to Objects for the remainder of the query expression (there's a lengthy article on this topic here).使用此结构创建一个新的 POCO(我假设数据类型为 DateTime):
然后将值分配给 SetModified,按如下方式更改代码:
注意我正在使用 UserProductVM 而不是 < strong>ProductVM 和 SetModified 而不是修改。
然后,当您获得属性 Modified 时,新的 POCO 会将其作为您格式化的字符串。
Create a new POCO with this structure (I'm assuming that the data type is DateTime):
Then you assign the value to SetModified, changing your code like this:
Pay attention i'm using UserProductVM instead ProductVM and SetModified instead modified.
Then when you get the property Modified, the new POCO is gonna bring it as the string that you formatted.
这可能不会增加很多,但以防万一有人像我一样疯狂,如果您需要使用 DatePart/DateName 构建 Zim 博士的答案的表达式树(包括时间部分),这里是完整的代码。显然,出于其他目的,您可以更改Product->YourInitialType、ProductVM->YourResultType 和modified->YourProperty。
编辑 (1/23/08): 由此生成的 SQL 在 6.0.2 和 6.1.3 之间发生了变化。最初,如果该值为 null,则生成的 SQL 将创建 null 结果。我认为在这种情况下这是理想的,但我可以理解为什么在其他情况下这是不需要的(null +“字符串值”= null)并且可能导致输出不等于您想要的。我将在下面详细介绍列输出如何更改,但问题在于现在将为空值输出“// ::”。我只是在调用代码中将这个输出作为特殊情况处理,然后手动将其更改回 null,但其他人可能希望解决添加更可靠的结果以确保 null 输出为 null 的问题。另外值得注意的是,新版本中的SQL语句非常长。
This may not add a lot, but just in case anyone is as crazy as me, here is the full code if you need to build the expression tree for Dr. Zim's answer using DatePart/DateName including the time part as well. Obviously, for other purposes you can change Product->YourInitialType, ProductVM->YourResultType, and modified->YourProperty.
Edit (1/23/08): The SQL generated from this changed between 6.0.2 and 6.1.3. Initially, if the value were null, the SQL generated would create a null result. I considered this desirable in this case, but I can see why in other cases it would not be desired (null + "a string value" = null) and could cause output not equal to what you would prefer. I'll detail how the column output changed below, but the rub of it is that this now will output "// ::" for null values. I simply handled this output in my calling code as a special case and manually changed it back to null, but others may want to tackle adding more robust results to ensure nulls output as null. It is also worth noting that the SQL statement is very long in the new version.