次の方法で共有


How to: Execute Data Service Queries (ADO.NET Data Services)

ADO.NET Data Services enables you to query a data service from a .NET Framework-based client application by using the generated client data service classes. You can execute queries by using one of these methods:

  • Executing a LINQ query against the named DataServiceQuery that you obtain from the DataServiceContext that the Add Data Service Reference tool generates.

  • (Implicitly) by enumerating over the named DataServiceQuery that you obtain from the DataServiceContext that the Add Data Service Reference tool generates.

  • (Explicitly) by calling the Execute``1(Uri) method on the DataServiceQuery, or the BeginExecute(AsyncCallback, Object) method for asynchronous execution.

For more information, see Querying the Data Service (ADO.NET Data Services).

The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the ADO.NET Data Services quickstart.

Example

The following example shows how to define and execute a LINQ query that returns all Customers against the Northwind data service.

' 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 Customers 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 (Customers customer in allCustomers)
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (Exception ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

The following example shows how to use the context that the Add Data Service Reference tool generates to implicitly execute a query that returns all Customers against the Northwind data service. The URI of the requested Customers entity set is determined automatically by the context. The query is executed implicitly when the enumeration occurs.

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

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

Try
    ' Enumerate over the query result, which is executed implicitly.
    For Each customer As Customers 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<Customers> query = context.Customers;

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

The following example shows how to use the DataServiceContext to explicitly execute a query that returns all Customers against the Northwind data service.

' 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 Customers In context.Execute(Of Customers)(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 (Customers customer in context.Execute<Customers>(customersUri))
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (Exception ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

See Also

Other Resources

How to: Add Query Options to a Data Service Query (ADO.NET Data Services)