我正在尝试在 Linq to Entities 中创建一个查询。我希望它返回包含从我正在查询的表中的字符串派生的 DateTime 属性的对象。数据(在 SQL Server 中)有一个名为 date_occurred 的字符串日期字段(在数据库中显示为 VARCHAR(8))。它有一个名为 time_occurred 的字符串时间字段 (varchar(6))。
date_occurred 的内容示例为“20131007”,表示 2013 年 10 月 7 日。time_occurred 的内容示例为“145710”,表示下午 2:57 后 10 秒。
我尝试了两种不起作用的方法:
Dim ATQuery = From e In EntityContext.vwSecAppAuditToday
Order By e.date_occurred, e.time_occurred
Select New AuditEntry With {
.EventTime = DateTime.ParseExact(Trim(e.date_occurred) & Trim(e.time_occurred), "yyyyMMddHHmmss", CultureInfo.InvariantCulture),
.ServerName = e.server_name
}
这会引发一条NotSupportedException
消息,说明:“LINQ to Entities 无法识别方法 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' 方法,并且此方法无法转换为存储表达式。”
在此之前,我尝试过:
Dim ATQuery = From e In EntityContext.vwSecAppAuditToday
Order By e.date_occurred, e.time_occurred
Select New AuditEntry With {
.EventTime = New DateTime(Integer.Parse(e.date_occurred.Substring(0, 4)),
Integer.Parse(e.date_occurred.Substring(4, 2)),
Integer.Parse(e.date_occurred.Substring(6, 2)),
Integer.Parse(e.time_occurred.Substring(0, 2)),
Integer.Parse(e.time_occurred.Substring(2, 2)),
Integer.Parse(e.time_occurred.Substring(4, 2))),
.ServerName = e.server_name
}
这也会抛出一个NotSupportedException
. 在这种情况下,消息指出:“LINQ to Entities 仅支持无参数构造函数和初始化程序。”
我正在尝试使用 Linq to Entities 做的事情吗?
编辑:评论提醒
对于那些后来阅读这篇文章的人,Moho 和 Matt Johnson 发表了特别有用的评论。我用+1标记了这些。