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

Definice

Vytvoří nový DataServiceQuery<TElement> s možností dotazu nastavenou v identifikátoru URI vygenerovaném vráceným dotazem.

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

Hodnota řetězce, která obsahuje název možnosti řetězce dotazu, která se má přidat.

value
Object

Objekt, který obsahuje hodnotu možnosti řetězce dotazu.

Návraty

Nový dotaz, který obsahuje požadovanou možnost dotazu připojenou k identifikátoru URI zadaného dotazu.

Příklady

Následující příklad ukazuje DataServiceQuery<TElement> , který se používá s voláním sekvenční AddQueryOption metody pouze k vrácení objednávek s náklady na dopravu vyšší než 30 USD a k seřazení výsledků podle data expedice v sestupném pořadí.

// 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

Následující příklad ukazuje, jak vytvořit dotaz LINQ, který je ekvivalentní předchozímu dotazu, který používal 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

Poznámky

Možnosti dotazu se přidají do výsledného identifikátoru URI pomocí ?name=value&name2=value2... syntaxe, kde se název mapuje přímo na name parametr a hodnota value je získána voláním ToString v parametru value . Začíná name na $.

Syntaxe WCF Data Services nezačíná na $. Pomocí této metody je možné přidat jiné než WCF Data Services možnosti dotazu. Pokud se nejedná o možnost WCF Data Services dotazu, je legální přidat stejnou možnost dotazu dvakrát. Pokud je přidána možnost dotazu, která již existuje v podkladovém identifikátoru URI, vyvolá se výjimka.

Platí pro