LINQ Considerations (WCF Data Services)

This topic provides information about the way in which LINQ queries are composed and executed when you are using the WCF Data Services client and limitations of using LINQ to query a data service that implements the Open Data Protocol (OData). For more information about composing and executing queries against an OData-based data service, see Querying the Data Service (WCF Data Services).

LINQ enables you to compose queries against a collection of objects that implements IEnumerable(Of T). Both the Add Service Reference dialog box in Visual Studio and the DataSvcUtil.exe tool are used to generate a representation of an OData service as an entity container class that inherits from DataServiceContext, as well as objects that represent the entities returned in feeds. These tools also generate properties on the entity container class for the collections that are exposed as feeds by the service. Each of these properties of the class that encapsulates the data service return a DataServiceQuery(Of TElement). Because the DataServiceQuery(Of TElement) class implements the IQueryable(Of T) interface defined by LINQ, you can compose a LINQ query against feeds exposed by the data service, which are translated by the client library into a query request URI that is sent to the data service on execution.

Important note Important

The set of queries expressible in the LINQ syntax is broader than those enabled in the URI syntax that is used by OData data services. A NotSupportedException is raised when the query cannot be mapped to a URI in the target data service. For more information, see the Unsupported LINQ Methods in this topic.

The following example is a LINQ query that returns Orders that have a freight cost of more than $30 and sorts the results by the shipping date, starting with the latest ship date:

Dim selectedOrders = From o In context.Orders _
        Where (o.Freight > 30) _
        Order By o.ShippedDate Descending _
        Select o

This LINQ query is translated into the following query URI that is executed against the Northwind-based quickstart data service:

http://localhost:12345/Northwind.svc/Orders?Orderby=ShippedDate&?filter=Freight gt 30

For more general information about LINQ, see LINQ (Language-Integrated Query).

LINQ enables you to compose queries by using both the language-specific declarative query syntax, shown in the previous example, as well as a set of query methods known as standard query operators. An equivalent query to the previous example can be composed by using only the method-based syntax, as shown the following example:

Dim selectedOrders = context.Orders _
                     .Where(Function(o) o.Freight.Value > 30) _
                     .OrderByDescending(Function(o) o.ShippedDate)

The WCF Data Services client is able to translate both kinds of composed queries into a query URI, and you can extend a LINQ query by appending query methods to a query expression. When you compose LINQ queries by appending method syntax to a query expression or a DataServiceQuery(Of TElement), the operations are added to the query URI in the order in which methods are called. This is equivalent to calling the AddQueryOption method to add each query option to the query URI.

Certain LINQ query methods, such as First(Of TSource)(IEnumerable(Of TSource)) or Single(Of TSource)(IEnumerable(Of TSource)), when appended to the query, cause the query to be executed. A query is also executed when results are enumerated implicitly, such as during a foreach loop or when the query is assigned to a List collection. For more information, see Querying the Data Service (WCF Data Services).

The client executes a LINQ query in two parts. Whenever possible, LINQ expressions in a query are first evaluated on the client, and then a URI-based query is generated and sent to the data service for evaluation against data in the service. For more information, see the section Client versus Server Execution in Querying the Data Service (WCF Data Services).

When a LINQ query cannot be translated in an OData-compliant query URI, an exception is raised when execution is attempted. For more information, see Querying the Data Service (WCF Data Services).

The examples in the following sections demonstrate the kinds of LINQ queries that can be executed against an OData service.

The LINQ query examples in this section filter data in the feed returned by the service.

The following examples are equivalent queries that filter the returned Orders entities so that only orders with a freight cost greater than $30 are returned:

  • Using LINQ query syntax:

    Dim filteredOrders = From o In context.Orders
                            Where o.Freight.Value > 30
                            Select o
    
  • Using LINQ query methods:

    Dim filteredOrders = context.Orders.Where(Function(o) o.Freight.Value > 0)
    
  • The URI query string $filter option:

    ' Define a query for orders with a Freight value greater than 30. 
    Dim filteredOrders _
                = context.Orders.AddQueryOption("$filter", "Freight gt 30M")
    

All of the previous examples are translated to the query URI: http://localhost:12345/northwind.svc/Orders()?$filter=Freight gt 30M.

The following examples show equivalent queries that sort returned data both by the company name and by postal code, descending:

  • Using LINQ query syntax:

    Dim sortedCustomers = From c In context.Customers
                                 Order By c.CompanyName Ascending,
                                 c.PostalCode Descending
                                 Select c
    
  • Using LINQ query methods:

    Dim sortedCustomers = context.Customers.OrderBy(Function(c) c.CompanyName) _
    .ThenByDescending(Function(c) c.PostalCode)
    
  • URI query string $orderby option):

    Dim sortedCustomers = context.Customers _
                          .AddQueryOption("$orderby", "CompanyName, PostalCode desc")
    

All of the previous examples are translated to the query URI: http://localhost:12345/northwind.svc/Customers()?$orderby=CompanyName,PostalCode desc.

The following examples show equivalent queries that project returned data into the narrower CustomerAddress type:

  • Using LINQ query syntax:

    Dim projectedQuery = From c In context.Customers
                         Select New CustomerAddress With
                        {
                            .CustomerID = c.CustomerID,
                            .Address = c.Address,
                            .City = c.City,
                            .Region = c.Region,
                            .PostalCode = c.PostalCode,
                            .Country = c.Country
                        }
    
  • Using LINQ query methods:

    Dim projectedQuery = context.Customers.Where(Function(c) c.Country = "Germany") _
                .Select(Function(c) New CustomerAddress With
                {
                    .CustomerID = c.CustomerID,
                    .Address = c.Address,
                    .City = c.City,
                    .Region = c.Region,
                    .PostalCode = c.PostalCode,
                    .Country = c.Country
                })
    
Note Note

The $select query option cannot be added to a query URI by using the AddQueryOption method. We recommend that you use the LINQ Select method to have the client generate the $select query option in the request URI.

Both of the previous examples are translated to the query URI: "http://localhost:12345/northwind.svc/Customers()?$filter=Country eq 'GerGerm'&$select=CustomerID,Address,City,Region,PostalCode,Country".

The following examples show equivalent queries that request a page of sorted order entities that includes 25 orders, skipping the first 50 orders:

  • Applying query methods to a LINQ query:

    Dim pagedOrders = (From o In context.Orders
                       Order By o.OrderDate Descending
                       Select o) _
                   .Skip(50).Take(25)
    
  • URI query string $skip and $top options):

    Dim pagedOrders = context.Orders _
                      .AddQueryOption("$orderby", "OrderDate desc") _
                      .AddQueryOption("$skip", 50) _
                      .AddQueryOption("$top", 25) _
    

Both of the previous examples are translated to the query URI: http://localhost:12345/northwind.svc/Orders()?$orderby=OrderDate desc&$skip=50&$top=25.

When you query an OData data service, you can request that entities related to the entity targeted by the query be included the returned feed. The Expand method is called on the DataServiceQuery(Of TElement) for the entity set targeted by the LINQ query, with the related entity set name supplied as the path parameter. For more information, see Loading Deferred Content (WCF Data Services).

The following examples show equivalent ways to use the Expand method in a query:

  • In LINQ query syntax:

    Dim ordersQuery = From o In context.Orders.Expand("Order_Details")
                         Where o.CustomerID = "ALFKI" 
                         Select o
    
  • With LINQ query methods:

    Dim ordersQuery = context.Orders.Expand("Order_Details") _
                              .Where(Function(o) o.CustomerID = "ALFKI")
    

Both of the previous examples are translated to the query URI: http://localhost:12345/northwind.svc/Orders()?$filter=CustomerID eq 'ALFKI'&$expand=Order_Details.

The following table contains the classes of LINQ methods are not supported and cannot be included in a query executed against an OData service:

Operation Type 

Unsupported Method

Set operators

All set operators are unsupported against a DataServiceQuery(Of TElement), which included the following:

Ordering operators

The following ordering operators that require IComparer(Of T) are unsupported against a DataServiceQuery(Of TElement):

Projection and filtering operators

The following projection and filtering operators that accept a positional argument are unsupported against a DataServiceQuery(Of TElement):

Grouping operators

All grouping operators are unsupported against a DataServiceQuery(Of TElement), including the following:

Grouping operations must be performed on the client.

Aggregate operators

All aggregate operations are unsupported against a DataServiceQuery(Of TElement), including the following:

Aggregate operations must either be performed on the client or be encapsulated by a service operation.

Paging operators

The following paging operators are not supported against a DataServiceQuery(Of TElement):

Note Note

Paging operators that are executed on an empty sequence return null.

Other operators

The following other operators are not supported against a DataServiceQuery(Of TElement):

  1. Empty(Of TResult)

  2. Range

  3. Repeat(Of TResult)

  4. ToDictionary

  5. ToLookup

The following common-language runtime (CLR) methods and properties are supported because they can be translated in a query expression for inclusion in the request URI to an OData service:

String Member

Supported OData Function

Concat(String, String)

string concat(string p0, string p1)

Contains(String)

bool substringof(string p0, string p1)

EndsWith(String)

bool endswith(string p0, string p1)

IndexOf(String, Int32)

int indexof(string p0, string p1)

Length

int length(string p0)

Replace(String, String)

string replace(string p0, string find, string replace)

Substring(Int32)

string substring(string p0, int pos)

Substring(Int32, Int32)

string substring(string p0, int pos, int length)

ToLower

string tolower(string p0)

ToUpper

string toupper(string p0)

Trim

string trim(string p0)

DateTime Member1

Supported OData Function

Day

int day(DateTime p0)

Hour

int hour(DateTime p0)

Minute

int minute(DateTime p0)

Month

int month(DateTime p0)

Second

int second(DateTime p0)

Year

int year(DateTime p0)

1The equivalent date and time properties of Microsoft.VisualBasic.DateAndTime, as well as the DatePart method in Visual Basic are also supported.

Math Member

Supported OData Function

Ceiling(Decimal)

decimal ceiling(decimal p0)

Ceiling(Double)

double ceiling(double p0)

Floor(Decimal)

decimal floor(decimal p0)

Floor(Double)

double floor(double p0)

Round(Decimal)

decimal round(decimal p0)

Round(Double)

double round(double p0)

Expression Member

Supported OData Function

TypeIs(Expression, Type)

bool isof(type p0)

The client may also be able to evaluate additional CLR functions on the client. A NotSupportedException is raised for any expression that cannot be evaluated on the client and cannot be translated into a valid request URI for evaluation on the server.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft