DataServiceQuery<TElement>.AddQueryOption(String, Object) Metoda

Definicja

Tworzy nowy DataServiceQuery<TElement> element z zestawem opcji zapytania w identyfikatorze URI wygenerowanym przez zwrócone zapytanie.

public:
 System::Data::Services::Client::DataServiceQuery<TElement> ^ AddQueryOption(System::String ^ name, System::Object ^ value);
public System.Data.Services.Client.DataServiceQuery<TElement> AddQueryOption (string name, object value);
member this.AddQueryOption : string * obj -> System.Data.Services.Client.DataServiceQuery<'Element>
Public Function AddQueryOption (name As String, value As Object) As DataServiceQuery(Of TElement)

Parametry

name
String

Wartość ciągu zawierająca nazwę opcji ciągu zapytania do dodania.

value
Object

Obiekt, który zawiera wartość opcji ciągu zapytania.

Zwraca

Nowe zapytanie, które zawiera żądaną opcję zapytania dołączono do identyfikatora URI podanego zapytania.

Przykłady

W poniższym przykładzie pokazano DataServiceQuery<TElement> , że jest używany z sekwencyjnymi AddQueryOption wywołaniami metody w celu zwrócenia tylko zamówień z kosztem ładunku większym niż 30 USD i kolejnością wyników według daty wysyłki w kolejności malejącej.

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

// Define a query for orders with a Freight value greater than 30
// and that is ordered by the ship date, descending.
DataServiceQuery<Order> selectedOrders = context.Orders
    .AddQueryOption("$filter", "Freight gt 30")
    .AddQueryOption("$orderby", "OrderID desc");

try
{
    // Enumerate over the results of the query.
    foreach (Order order in selectedOrders)
    {
        Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}",
            order.OrderID, order.ShippedDate, order.Freight);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define a query for orders with a Freight value greater than 30
' and that is ordered by the ship date, descending.
Dim selectedOrders As DataServiceQuery(Of Order) = context.Orders _
.AddQueryOption("$filter", "Freight gt 30") _
.AddQueryOption("$orderby", "OrderID desc")

Try
    ' Enumerate over the results of the query.
    For Each order As Order In selectedOrders
        Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}", _
                order.OrderID, order.ShippedDate, order.Freight)
    Next
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try

W poniższym przykładzie pokazano, jak utworzyć zapytanie LINQ, które jest równoważne poprzedniemu zapytaniu, które używało .AddQueryOption

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

// Define a query for orders with a Freight value greater than 30
// and that is ordered by the ship date, descending.
var selectedOrders = from o in context.Orders
                     where o.Freight > 30
                     orderby o.ShippedDate descending
                     select o;

try
{
    // Enumerate over the results of the query.
    foreach (Order order in selectedOrders)
    {
        Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}",
            order.OrderID, order.ShippedDate, order.Freight);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define a query for orders with a Freight value greater than 30
' and that is ordered by the ship date, descending.
Dim selectedOrders = From o In context.Orders _
        Where (o.Freight > 30) _
        Order By o.ShippedDate Descending _
        Select o

Try
    ' Enumerate over the results of the query.
    For Each order As Order In selectedOrders
        Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}", _
                order.OrderID, order.ShippedDate, order.Freight)
    Next
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try

Uwagi

Opcje zapytania są dodawane do wynikowego identyfikatora URI przy użyciu ...?name=value&name2=value2 składnia, w której nazwa jest mapowana bezpośrednio na name parametr , a value parametr jest uzyskiwany przez wywołanie metody ToString dla parametru value . Rozpoczyna name się od $.

Składnia nie Usługi danych programu WCF nie rozpoczyna się od $. Opcje zapytań innych niż Usługi danych programu WCF można dodać przy użyciu tej metody. Istnieje prawo, aby dodać tę samą opcję zapytania dwa razy, jeśli opcja nie jest opcją Usługi danych programu WCF zapytania. Jeśli zostanie dodana opcja zapytania, która jest już obecna w bazowym identyfikatorze URI, zgłaszany jest wyjątek.

Dotyczy