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

Definizione

Crea un nuovo oggetto DataServiceQuery<TElement> con il set di opzioni query nell'URI generato dalla query restituita.

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)

Parametri

name
String

Valore stringa che contiene il nome dell'opzione della stringa di query da aggiungere.

value
Object

Oggetto che contiene il valore dell'opzione della stringa di query.

Restituisce

Nuova query che include l'opzione di query richiesta aggiunta all'URI della query specificata.

Esempio

Nell'esempio seguente viene illustrato l'utilizzo di un oggetto DataServiceQuery<TElement> con chiamate del metodo AddQueryOption sequenziali per restituire solo gli ordini con un costo spedizione maggiore di 30 dollari ed eseguire l'ordinamento dei risultati in senso decrescente in base alla data di spedizione.

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

Nell'esempio seguente viene illustrato come creare una query LINQ equivalente alla query precedente utilizzata dal metodo 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

Commenti

Le opzioni di query vengono aggiunte all'URI risultante usando ?name=value&name2=value2... sintassi in cui il nome esegue il mapping direttamente al name parametro e viene value ottenuto chiamando ToString nel value parametro. Il parametro name inizia con $.

La sintassi non WCF Data Services non inizia con $. È possibile aggiungere opzioni di query non WCF Data Services usando questo metodo. È legale aggiungere la stessa opzione di query due volte se l'opzione non è un'opzione di query WCF Data Services. Se viene aggiunta un'opzione query che è già presente nell'URI sottostante, viene generata un'eccezione.

Si applica a