共用方式為


HOW TO:執行資料服務查詢 (WCF Data Services)

WCF Data Services 可讓您使用所產生的用戶端資料服務類別,從以 .NET Framework 為基礎的用戶端應用程式查詢資料服務。 您可以使用下列其中一種方法執行查詢:

  • 針對您從 Add Data Service Reference 工具所產生之 DataServiceContext 取得的具名 DataServiceQuery 執行 LINQ 查詢。

  • 以隱含的方式列舉您從 Add Data Service Reference 工具所產生之 DataServiceContext 取得的具名 DataServiceQuery

  • 透過在 DataServiceQuery 呼叫 Execute 方法或 BeginExecute 方法,明確地進行非同步執行。

如需詳細資訊,請參閱查詢資料服務 (WCF Data Services)

本主題的範例使用 Northwind 範例資料服務,以及自動產生的用戶端資料服務類別。 此服務和用戶端資料類別會在您完成 WCF Data Services 快速入門時建立。

範例

下列範例示範如何定義與執行針對 Northwind 資料服務傳回所有 Customers 的 LINQ 查詢。

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

Try
    ' Define a LINQ query that returns all customers.
    Dim allCustomers = From cust In context.Customers _
                           Select cust

    ' Enumerate over the query obtained from the context.
    For Each customer As Customer In allCustomers
        Console.WriteLine("Customer Name: {0}", customer.CompanyName)
    Next
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);

try
{
    // Define a LINQ query that returns all customers.
    var allCustomers = from cust in context.Customers
                       select cust;

    // Enumerate over the query obtained from the context.
    foreach (Customer customer in allCustomers)
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

下列範例示範如何使用 Add Data Service Reference 工具所產生的內容,隱含地執行會針對 Northwind 資料服務傳回所有 Customers 的查詢。 內容會自動判斷所要求之 Customers 實體集的 URI。 發生列舉時會隱含執行查詢。

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define a new query for Customers.
Dim query As DataServiceQuery(Of Customer) = context.Customers

Try
    ' Enumerate over the query result, which is executed implicitly.
    For Each customer As Customer In query
        Console.WriteLine("Customer Name: {0}", customer.CompanyName)
    Next
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);

// Define a new query for Customers.
DataServiceQuery<Customer> query = context.Customers;

try
{
    // Enumerate over the query result, which is executed implicitly.
    foreach (Customer customer in query)
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

下列範例示範如何使用 DataServiceContext 明確地執行針對 Northwind 資料服務傳回所有 Customers 的查詢。

' Define a request URI that returns Customers.
Dim customersUri = New Uri(svcUri, "Northwind.svc/Customers")

' Create the DataServiceContext using the service URI.
Dim context = New DataServiceContext(svcUri)

Try
    ' Enumerate over the query result.
    For Each customer As Customer In context.Execute(Of Customer)(customersUri)
        Console.WriteLine("Customer Name: {0}", customer.CompanyName)
    Next

Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try
// Define a request URI that returns Customers.
Uri customersUri = new Uri(svcUri, "Northwind.svc/Customers");

// Create the DataServiceContext using the service URI.
DataServiceContext context = new DataServiceContext(svcUri);

try
{
    // Enumerate over the query result.
    foreach (Customer customer in context.Execute<Customer>(customersUri))
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

另請參閱

工作

HOW TO:將查詢選項加入至資料服務查詢 (WCF Data Services)