共用方式為


DataServiceContext.Execute<TElement> 方法 (Uri)

將要求傳送到資料服務來執行特定的 URI。

不受到適用於 Silverlight 的 WCF Data Services 5.0 用戶端支援。

命名空間:  System.Data.Services.Client
組件:  Microsoft.Data.Services.Client (在 Microsoft.Data.Services.Client.dll 中)

語法

'宣告
Public Function Execute(Of TElement) ( _
    requestUri As Uri _
) As IEnumerable(Of TElement)
'用途
Dim instance As DataServiceContext
Dim requestUri As Uri
Dim returnValue As IEnumerable(Of TElement)

returnValue = instance.Execute(requestUri)
public IEnumerable<TElement> Execute<TElement>(
    Uri requestUri
)
public:
generic<typename TElement>
IEnumerable<TElement>^ Execute(
    Uri^ requestUri
)
member Execute : 
        requestUri:Uri -> IEnumerable<'TElement> 
JScript 不支援泛型型別及方法。

型別參數

  • TElement
    查詢所傳回的型別。

參數

  • requestUri
    型別:System.Uri
    查詢要求將傳送至的 URI。URI 可以是任何有效的資料服務 URI;它能包含 $ 查詢參數。

傳回值

型別:System.Collections.Generic.IEnumerable<TElement>
查詢作業的結果。

例外狀況

例外狀況 條件
WebException

未收到 requestUri 要求的回應時。

ArgumentNullException

當 requestUri 為 nullnull 參考 (在 Visual Basic 中為 Nothing) 時。

ArgumentException

當 requestUri 不是資料服務的有效 URI 時。

InvalidOperationException

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

DataServiceQueryException

當資料服務傳回「HTTP 404:找不到資源」錯誤時。

備註

Execute 方法用於依 URI 查詢資料服務;該方法會導致 HTTP GET 要求發出給資料服務。 指定的要求 URI 可以是絕對或相對 URI。

如果 requestUri 是絕對 URI,這個方法會驗證 URI 是否指向建構 DataServiceContext 時所指定的相同資料服務。 如果 requestUri 是相對,此方法會移除任何前導斜線並將 requestUri 附加到建構 DataServiceContext 時提供的路徑。 在傳遞至 DataServiceContext 建構函式的 URI 後面會附加斜線 (若沒有的話)。

當此方法傳回時,已從網路資料流讀取要求的所有 HTTP 回應,但尚未處理回應;不會識別解析或將物件具體化。 除非回應中的指定之實體經過列舉,否則不會對該實體進行任何識別解析或完整物件具體化。

範例

此範例使用 do?while 迴圈,從資料服務的分頁式結果載入 Customers 實體。 透過使用下一個連結 URI 呼叫 Execute 方法以接收下一頁資料。

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Dim token As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0

Try
    ' Execute the query for all customers and get the response object.
    Dim response As QueryOperationResponse(Of Customer) = _
        CType(context.Customers.Execute(), QueryOperationResponse(Of Customer))

    ' With a paged response from the service, use a do...while loop 
    ' to enumerate the results before getting the next link.
    Do
        ' Write the page number.
        Console.WriteLine("Page {0}:", pageCount + 1)

        ' If nextLink is not null, then there is a new page to load.
        If token IsNot Nothing Then
            ' Load the new page from the next link URI.
            response = CType(context.Execute(Of Customer)(token),  _
            QueryOperationResponse(Of Customer))
        End If

        ' Enumerate the customers in the response.
        For Each customer As Customer In response
            Console.WriteLine(vbTab & "Customer Name: {0}", customer.CompanyName)
        Next

        ' Get the next link, and continue while there is a next link.
        token = response.GetContinuation()
    Loop While token IsNot Nothing
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> token = null;
int pageCount = 0; 

try
{ 
    // Execute the query for all customers and get the response object.
    QueryOperationResponse<Customer> response =
        context.Customers.Execute() as QueryOperationResponse<Customer>;

    // With a paged response from the service, use a do...while loop 
    // to enumerate the results before getting the next link.
    do
    {
        // Write the page number.
        Console.WriteLine("Page {0}:", pageCount++);

        // If nextLink is not null, then there is a new page to load.
        if (token != null)
        {
            // Load the new page from the next link URI.
            response = context.Execute<Customer>(token)
                as QueryOperationResponse<Customer>;
        }

        // Enumerate the customers in the response.
        foreach (Customer customer in response)
        {
            Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
        }
    }

    // Get the next link, and continue while there is a next link.
    while ((token = response.GetContinuation()) != null);
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

請參閱

參考

DataServiceContext 類別

Execute 多載

System.Data.Services.Client 命名空間

其他資源

載入延後的內容 (WCF Data Services)

HOW TO:載入分頁結果 (WCF Data Services)