Udostępnij za pośrednictwem


DataServiceQuery<TElement>.AddQueryOption Method

Creates a new DataServiceQuery<TElement> with the query option set in the URI generated by the returned query.

Namespace:  System.Data.Services.Client
Assembly:  Microsoft.Data.Services.Client (in Microsoft.Data.Services.Client.dll)

Syntax

'Declaration
Public Function AddQueryOption ( _
    name As String, _
    value As Object _
) As DataServiceQuery(Of TElement)
'Usage
Dim instance As DataServiceQuery 
Dim name As String 
Dim value As Object 
Dim returnValue As DataServiceQuery(Of TElement)

returnValue = instance.AddQueryOption(name, _
    value)
public DataServiceQuery<TElement> AddQueryOption(
    string name,
    Object value
)
public:
DataServiceQuery<TElement>^ AddQueryOption(
    String^ name, 
    Object^ value
)
member AddQueryOption : 
        name:string * 
        value:Object -> DataServiceQuery<'TElement> 
public function AddQueryOption(
    name : String, 
    value : Object
) : DataServiceQuery<TElement>

Parameters

  • name
    Type: System.String
    The string value that contains the name of the query string option to add.
  • value
    Type: System.Object
    The object that contains the value of the query string option.

Return Value

Type: System.Data.Services.Client.DataServiceQuery<TElement>
A new query that includes the requested query option appended to the URI of the supplied query

Remarks

The query options are added to the resultant URI using ?name=value&name2=value2… syntax where the name maps directly to the name parameter and the value is obtained by calling ToString on the value parameter. The name starts with $.

Non-WCF Data Services syntax does not start with $. Non-WCF Data Services query options can be added using this method. It is legal to add the same query option twice if the option is not an WCF Data Services query option. If a query option is added that is already present in the underlying URI, an exception is thrown.

The $select query option cannot be added to a query URI by using the AddQueryOption(String, Object) method. We recommend that you use the LINQ Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>) method and have the client generate the $select query option in the request URI.

Examples

The following example shows a DataServiceQuery<TElement> that is used with sequential AddQueryOption method calls to only return orders with a freight cost of more than $30 and to order the results by the ship date in descending order.

' 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
// 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);
}

The following example shows how to compose a LINQ query that is equivalent to the previous query that used AddQueryOption.

' 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
// 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);
}

See Also

Reference

DataServiceQuery<TElement> Class

System.Data.Services.Client Namespace