LINQ to Entities exceptions (ElementAtOrDefault and CompareObjectEqual)
Posted
by OffApps Cory
on Stack Overflow
See other posts from Stack Overflow
or by OffApps Cory
Published on 2010-04-05T19:27:03Z
Indexed on
2010/04/05
19:33 UTC
Read the original article
Hit count: 854
I am working on a shipping platform which will eventually automate shipping through several major carriers. I have a ShipmentsView Usercontrol which displayes a list of Shipments (returned by EntityFramework), and when a user clicks on a shipment item, it spawns a ShipmentEditView and passes the ShipmentID (RecordKey) to that view.
I initially wrestled with trying to get the context from the parent (ShipmentsView) and finally gave up resolving to get to it later. I wanted to do this to keep a single instance of the context. anyhow, I now create a new instance of the context in my ShipmentEditViewModel, and query against it for the Shipment record. I know I could just pass the record, but I wanted to use the Ocean Framework that Karl Shifflett wrote and don't want to muck about writing new transition methods.
So anyhow, I query and when stepping through, I can see that it returns a record, as soon as execution reached the point where it assigned the query result to the e.Result property, it throws up the following exception depending on the query I used.
LINQToEntities
Dim RecordID As Decimal = CDec(e.Argument)
Dim myResult = From ship In _Context.Shipment _
Where ship.ShipID = e.Argument _
Select ship
Select Case myResult.Count
Case 0
e.Result = New Shipment
Case 1
e.Result = myResult(0)
Case Else
e.Result = Nothing
End Select
"LINQ to Entities does not recognize the method 'System.Object.CompareObjectEqual(System.Object, System.Object, Boolean)' method, and this method cannot be translated into a store expression.
LINQToEntities via Method calls
Dim RecordID As Decimal = CDec(e.Argument)
Dim myResult = _Context.Shipment.Where(Function(s) s.ShipID = RecordID)
Select Case myResult.Count
Case 0
e.Result = New Shipment
Case 1
e.Result = myResult(0)
Case Else
e.Result = Nothing
End Select
LINQ to Entities does not recognize the method 'SnazzyShippingDAL.Shipment ElementAtOrDefault[Shipment] (System.Linq.IQueryable`1[SnazzyShippingDAL.Shipment], Int32)' method, and this method cannot be translated into a store expression.
I have been trying to get this thing to display a record for like three days. i am seriously thinking about going back and re=-engineering it without the MVVM pattern (which I realize I am only starting to learn and understand) if only to make the &$^%ed thing work. Any help will be muchly appreciated.
Cory
© Stack Overflow or respective owner