共用方式為


DataServiceContext.EndExecute<TElement>(IAsyncResult) 方法

定義

public:
generic <typename TElement>
 System::Collections::Generic::IEnumerable<TElement> ^ EndExecute(IAsyncResult ^ asyncResult);
public System.Collections.Generic.IEnumerable<TElement> EndExecute<TElement> (IAsyncResult asyncResult);
member this.EndExecute : IAsyncResult -> seq<'Element>
Public Function EndExecute(Of TElement) (asyncResult As IAsyncResult) As IEnumerable(Of TElement)

類型參數

TElement

查詢所傳回的型別。

參數

asyncResult
IAsyncResult

IAsyncResult 物件

傳回

IEnumerable<TElement>

查詢作業所傳回的結果。

例外狀況

asyncResultnull 時。

asyncResult 不是來自這個 DataServiceContext 執行個體時。

-或-

先前已呼叫 EndExecute<TElement>(IAsyncResult) 方法。

在執行要求期間或將回應訊息的內容轉換為物件時引發錯誤時。

範例

下列範例示範如何透過呼叫 BeginExecute 方法啟動查詢來執行非同步查詢。 內嵌委派會呼叫 EndExecute 方法以顯示查詢結果。 此範例會DataServiceContext根據 Northwind 數據服務使用 Add Service Reference 工具所產生的 ,當您完成 WCF Data Services 時就會建立此服務參考工具。

public static void BeginExecuteCustomersQuery()
{
    // Create the DataServiceContext using the service URI.
    NorthwindEntities context = new NorthwindEntities(svcUri);

    // Define the query to execute asynchronously that returns
    // all customers with their respective orders.
    DataServiceQuery<Customer> query = (DataServiceQuery<Customer>)(from cust in context.Customers.Expand("Orders")
                                       where cust.CustomerID == "ALFKI"
                                       select cust);

    try
    {
        // Begin query execution, supplying a method to handle the response
        // and the original query object to maintain state in the callback.
        query.BeginExecute(OnCustomersQueryComplete, query);
    }
    catch (DataServiceQueryException ex)
    {
        throw new ApplicationException(
            "An error occurred during query execution.", ex);
    }
}

// Handle the query callback.
private static void OnCustomersQueryComplete(IAsyncResult result)
{
    // Get the original query from the result.
    DataServiceQuery<Customer> query =
        result as DataServiceQuery<Customer>;

    foreach (Customer customer in query.EndExecute(result))
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
        foreach (Order order in customer.Orders)
        {
            Console.WriteLine("Order #: {0} - Freight $: {1}",
                order.OrderID, order.Freight);
        }
    }
}
Public Shared Sub BeginExecuteCustomersQuery()
    ' Create the DataServiceContext using the service URI.
    Dim context = New NorthwindEntities(svcUri)

    ' Define the delegate to callback into the process
    Dim callback As AsyncCallback = AddressOf OnCustomersQueryComplete

    ' Define the query to execute asynchronously that returns 
    ' all customers with their respective orders.
    Dim query As DataServiceQuery(Of Customer) = _
    context.Customers.Expand("Orders")

    Try
        ' Begin query execution, supplying a method to handle the response
        ' and the original query object to maintain state in the callback.
        query.BeginExecute(callback, query)
    Catch ex As DataServiceQueryException
        Throw New ApplicationException( _
                "An error occurred during query execution.", ex)
    End Try
End Sub
' Handle the query callback.
Private Shared Sub OnCustomersQueryComplete(ByVal result As IAsyncResult)
    ' Get the original query from the result.
    Dim query As DataServiceQuery(Of Customer) = _
        CType(result.AsyncState, DataServiceQuery(Of Customer))

    ' Complete the query execution.
    For Each customer As Customer In query.EndExecute(result)
        Console.WriteLine("Customer Name: {0}", customer.CompanyName)
        For Each order As Order In customer.Orders
            Console.WriteLine("Order #: {0} - Freight $: {1}", _
                    order.OrderID, order.Freight)
        Next
    Next
End Sub

備註

根據標準的開始-結束非同步模式,在擷取查詢結果時會叫用所提供的回呼。 如需詳細資訊,請參閱 異步操作

叫用回呼的時候,就會已經從 HTTP 資料流讀取所有結果,但是未加以處理,而且不會對任何本機使用者端的物件進行具體化或修改,也不會發生識別解析。 叫用 EndExecute 的時候,就會建立並傳回 DataServiceResponse,但是仍未處理結果。 只有當使用者列舉結果的時候,才會發生識別解析、物件具體化和管理。

適用於

另請參閱