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

Definizione

Crea un nuovo DataServiceQuery<TElement> oggetto con l'opzione query impostata 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 contenente il nome dell'opzione della stringa di query da aggiungere.

value
Object

Oggetto contenente il valore dell'opzione della stringa di query.

Valori restituiti

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

Esempio

Nell'esempio seguente viene illustrato un DataServiceQuery<TElement> oggetto utilizzato con chiamate di metodo sequenziale AddQueryOption per restituire solo gli ordini con un costo di trasporto superiore a $30 e ordinare i risultati in base alla data di spedizione in ordine decrescente.

// 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 comporre una query LINQ equivalente alla query precedente che ha usato 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 diretto al name parametro e viene value ottenuto chiamando ToString sul value parametro . Inizia name con $.

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

Si applica a