尝试将 LINQ-to-Entity 与日期进行比较会导致错误

发布于 2024-10-22 01:20:15 字数 670 浏览 1 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-10-29 01:20:15

问题出在你的查询中。 if 语句中会引发异常,因为查询的执行被推迟到需要运行时为止。问题在于

Session("people_code_id")

LINQ 查询中的所有内容都需要转换为 SQL 才能进行查询。实体框架无法将会话项 getter 转换为 SQL,因此会引发异常。尝试在查询之前将其取出到一个变量中,如下所示:(

Dim peopleCodeId = CStr(Session("people_code_id"))
Dim rhcexists = From p In dbContracts.Signatures _
                    Where p.StudentID = peopleCodeId _
                    And p.ContractType = "rhc" _
                    Order By p.ID Descending _
                    Select p

我的 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

Session("people_code_id")

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:

Dim peopleCodeId = CStr(Session("people_code_id"))
Dim rhcexists = From p In dbContracts.Signatures _
                    Where p.StudentID = peopleCodeId _
                    And p.ContractType = "rhc" _
                    Order By p.ID Descending _
                    Select p

(My VB.NET is a little rusty, but I think that's right)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文