views:

282

answers:

2

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

+1  A: 

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

Laurence Burke
Any feedback on this would be great Cory
Laurence Burke
It did indeed return the ID that I specified, and all seems good. Apparently one is not supposed to specify the object by index from the query results. The post by Craig worked by referencing myResult.First instead of myResult(0).thank you for your help though.
OffApps Cory
+1  A: 

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()
Craig Stuntz
That did it. Now if I can get the stupid form to display the data, I will be good. MOLE (the visualizer) tells me that the datacontext for the user controls (and everything on down the tree) is the viewmodel, which has a property ShipmentRecord that is the record returned from the above method.I have tried everything I can think of to get the Shipment property values into those textboxes, and to no avail! Mole tells me that the companyName textboxes datacontext is the ViewModel so it stands to reason that Text={Binding Path=ShipmentRecord.CompanyName} should work right? Grrr!!
OffApps Cory