尝试将 LINQ-to-Entity 与日期进行比较会导致错误
我有一个简单的 If..Then ,如下所示:
If rhcexists.First.SignatureDate > Date.Today.AddMonths(-6) Then
End If
rhcexists 是对实体模型的简单查询:
Dim rhcexists = From p In dbContracts.Signatures _
Where p.StudentID = CStr(Session("people_code_id")) _
And p.ContractType = "rhc" _
Order By p.ID Descending _
Select p
问题是此比较会导致错误:
LINQ to Entities 无法识别“System.Object get_Item(System.String)”方法,并且此方法无法转换为存储表达式。
这发生在 If 子句上。有什么想法为什么会发生这种情况以及如何解决它吗?
我正在使用 ASP.NET 4、EF 4 和 VS 2010 SP1。
I have a simple If..Then like so:
If rhcexists.First.SignatureDate > Date.Today.AddMonths(-6) Then
End If
rhcexists is a simple query to the Entity Model:
Dim rhcexists = From p In dbContracts.Signatures _
Where p.StudentID = CStr(Session("people_code_id")) _
And p.ContractType = "rhc" _
Order By p.ID Descending _
Select p
Problem is that this comparison results in an error:
LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression.
This is occurring on the If clause. Any ideas why this occurring and how I can fix it?
I am using ASP.NET 4, EF 4, and VS 2010 SP1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在你的查询中。 if 语句中会引发异常,因为查询的执行被推迟到需要运行时为止。问题在于
LINQ 查询中的所有内容都需要转换为 SQL 才能进行查询。实体框架无法将会话项 getter 转换为 SQL,因此会引发异常。尝试在查询之前将其取出到一个变量中,如下所示:(
我的 VB.NET 有点生疏,但我认为这是正确的)
The problem is in your query. The exception is thrown in your if statement because execution of the query is deferred until it needs to be run. The problem is with
Everything in the LINQ query needs to get translated into SQL in order to make the query. The Entity Framework cannot translate the Session Item getter into SQL, so it throws an exception. Try pulling that out into a variable before the query like this:
(My VB.NET is a little rusty, but I think that's right)