开发者

LINQ to Entities exceptions (ElementAtOrDefault and CompareObjectEqual)

开发者 https://www.devze.com 2022-12-25 12:52 出处:网络
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 EntityFra

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


This:

Dim RecordID As Decimal = CDec(e.Argument)
Dim myResult = From ship In _Context.Shipment _
               Where ship.ShipID = e.Argument _
               Select ship

...should be:

Dim RecordID As Decimal = CDec(e.Argument)
Dim myResult = From ship In _Context.Shipment _
               Where ship.ShipID = RecordID _
               Select ship

Because you want to compare the ID with an ID, not with an object.

This:

    Case 1
        e.Result = myResult(0)

Should be:

    Case 1
        e.Result = myResult.First()


I am not to familiar with vb but I am somewhat familiar with linq. I was wondering if you could execute a .FirstOrDefault() function. Its like executing a scalar stored procedure I am not to sure of the translation to vb but I know c# so I don't know if this helps because it seems like you might be having the problem of trying to pass the info to e.result but here it goes

    Dim myResult = (From ship In _Context.Shipment _
                   Where ship.ShipID = e.Argument _
                   Select ship.ShipID).FirstOrDefault()
    If myResult <> null
     Then e.Result = myResult
     Else e.Result = (function that creates a new shipment and returns the object back).ShipID

Hope this at least might be able to give you a spark of creativity or direction

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号