Private Sub ResetBindingData()
' Create a new data service context.
svcContext = _
New NorthwindEntities(New Uri("Northwind.svc", UriKind.Relative))
ordersBindingCollection.Clear()
detailsBindingCollection.Clear()
End Sub
' We need to persist the result of an operation
' to be able to invoke the dispatcher.
Private currentResult As IAsyncResult
Private Sub getCustomerOrders_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Instantiate the data service context and clear any existing bindings.
ResetBindingData()
' Define the delegate to callback into the process
Dim callback As AsyncCallback = AddressOf OnCustomerOrdersQueryComplete
' Define the filter condition based on the customer ID.
Dim filterExpression = "CustomerID eq '" & Me.customerId.Text & "'"
' Define a query to return the specifed customer and related orders.
Dim query As DataServiceQuery(Of Customers) = _
svcContext.Customers() _
.AddQueryOption("$filter", filterExpression).Expand("Orders")
Try
' Begin the query execution.
query.BeginExecute(callback, query)
Catch ex As Exception
messageTextBlock.Text = ex.Message
End Try
End Sub
Private Sub OnCustomerOrdersQueryComplete(ByVal result As IAsyncResult)
' Persist the query result for the delegate.
currentResult = result
' Use the Dispatcher to ensure that the
' asynchronous call returns in the correct thread.
Dispatcher.BeginInvoke(AddressOf QueryCompletedByDispatcher)
End Sub
Private Sub QueryCompletedByDispatcher()
Try
' Get the original query back from the result.
Dim query = CType(currentResult.AsyncState, DataServiceQuery(Of Customers))
Dim returnedCustomer = query.EndExecute(currentResult).FirstOrDefault()
If returnedCustomer IsNot Nothing Then
' Load the retuned orders into the binding collection.
For Each order As Orders In returnedCustomer.Orders
ordersBindingCollection.Add(order)
Next
' Bind the grid control to the collection and update the layout.
Me.ordersGrid.DataContext = ordersBindingCollection
Me.ordersGrid.UpdateLayout()
' Hide the relationship property columns.
Me.ordersGrid.Columns(8).Visibility = Visibility.Collapsed
Me.ordersGrid.Columns(11).Visibility = Visibility.Collapsed
Me.ordersGrid.Columns(12).Visibility = Visibility.Collapsed
End If
Catch ex As DataServiceQueryException
Me.messageTextBlock.Text = String.Format("Error: {0} - {1}", _
ex.Response.StatusCode.ToString(), ex.Response.Error.Message)
End Try
End Sub