Trying to Compare LINQ-to-Entity to Date Results in Error
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.
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)
精彩评论