다음을 통해 공유


방법: 데이터 서비스 쿼리 실행(WCF Data Services)

WCF Data Services 를 사용하면 생성된 클라이언트 데이터 서비스 클래스를 통해 .NET Framework 기반 클라이언트 응용 프로그램에서 데이터 서비스를 쿼리할 수 있습니다. 다음 방법 중 하나를 사용하여 쿼리를 실행할 수 있습니다.

  • Add Data Service Reference 도구가 생성하는 DataServiceContext에서 가져온 명명된 DataServiceQuery에 대해 LINQ 쿼리 실행

  • Add Data Service Reference 도구가 생성하는 DataServiceContext에서 가져온 명명된 DataServiceQuery를 열거하여 암시적으로

  • DataServiceQueryExecute 메서드를 호출하거나 비동기 실행의 경우 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);
}

참고 항목

작업

방법: 데이터 서비스 쿼리에 쿼리 옵션 추가(WCF Data Services)