DataServiceQuery<TElement>.AddQueryOption(String, Object) Método

Definición

Crea un nuevo DataServiceQuery<TElement> con la opción de consulta establecida en el URI generado por la consulta devuelta.

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)

Parámetros

name
String

Valor de cadena que contiene el nombre de la opción de cadena de consulta que se va a agregar.

value
Object

Objeto que contiene el valor de la opción de cadena de consulta.

Devoluciones

Nueva consulta que incluye la opción de consulta solicitada anexada al URI de la consulta proporcionada.

Ejemplos

El ejemplo siguiente muestra DataServiceQuery<TElement> que se utiliza con llamadas secuenciales al método AddQueryOption para devolver solamente los pedidos con un coste de transporte superior a 30 dólares y para ordenar los resultados por la fecha de transporte en orden descendente.

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

En el ejemplo siguiente se muestra cómo crear un consulta LINQ que es equivalente a la consulta anterior que utilizaba 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

Comentarios

Las opciones de consulta se agregan al URI resultante mediante ?name=value&name2=value2... sintaxis donde el nombre se asigna directamente al name parámetro y value se obtiene llamando a ToString en el value parámetro . name empieza con $.

La sintaxis no WCF Data Services no comienza por $. Las opciones de consulta que no son WCF Data Services se pueden agregar mediante este método. Es legal agregar la misma opción de consulta dos veces si la opción no es una opción de consulta WCF Data Services. Si se agrega una opción de consulta que ya está presente en el URI subyacente, se producirá una excepción.

Se aplica a