Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
  • None
User Action:
  • None

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.

LINQ enables you to compose queries against a collection of objects that implements IEnumerable<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<TElement>. Because the DataServiceQuery<TElement> class implements the IQueryable<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.

System_CAPS_ICON_important.jpg 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:

[!CODE [Astoria NorthwindClient#AddQueryOptionsLinqSpecific](Astoria NorthwindClient#AddQueryOptionsLinqSpecific)]

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:

[!CODE [Astoria NorthwindClient#AddQueryOptionsLinqExpressionSpecific](Astoria NorthwindClient#AddQueryOptionsLinqExpressionSpecific)]

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<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<TSource>(IEnumerable<TSource>) or Single<TSource>(IEnumerable<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.

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.

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.

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

Filtering

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:

    [!CODE [Astoria NorthwindClient#LinqWhereClauseSpecific](Astoria NorthwindClient#LinqWhereClauseSpecific)]

  • Using LINQ query methods:

    [!CODE [Astoria NorthwindClient#LinqWhereMethodSpecific](Astoria NorthwindClient#LinqWhereMethodSpecific)]

  • The URI query string $filter option:

    [!CODE [Astoria NorthwindClient#ExplicitQueryWhereMethodSpecific](Astoria NorthwindClient#ExplicitQueryWhereMethodSpecific)]

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

Sorting

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

  • Using LINQ query syntax:

    [!CODE [Astoria NorthwindClient#LinqOrderByClauseSpecific](Astoria NorthwindClient#LinqOrderByClauseSpecific)]

  • Using LINQ query methods:

    [!CODE [Astoria NorthwindClient#LinqOrderByMethodSpecific](Astoria NorthwindClient#LinqOrderByMethodSpecific)]

  • URI query string $orderby option):

    [!CODE [Astoria NorthwindClient#ExplicitQueryOrderByMethodSpecific](Astoria NorthwindClient#ExplicitQueryOrderByMethodSpecific)]

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

Projection

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

  • Using LINQ query syntax:

    [!CODE [Astoria NorthwindClient#LinqSelectClauseSpecific](Astoria NorthwindClient#LinqSelectClauseSpecific)]

  • Using LINQ query methods:

    [!CODE [Astoria NorthwindClient#LinqSelectMethodSpecific](Astoria NorthwindClient#LinqSelectMethodSpecific)]

System_CAPS_ICON_note.jpg 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<TSource, TResult> 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".

Client Paging

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:

    [!CODE [Astoria NorthwindClient#LinqSkipTakeMethodSpecific](Astoria NorthwindClient#LinqSkipTakeMethodSpecific)]

  • URI query string $skip and $top options):

    [!CODE [Astoria NorthwindClient#ExplicitQuerySkipTakeMethodSpecific](Astoria NorthwindClient#ExplicitQuerySkipTakeMethodSpecific)]

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

Expand

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

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

  • In LINQ query syntax:

    [!CODE [Astoria NorthwindClient#LinqQueryExpandSpecific](Astoria NorthwindClient#LinqQueryExpandSpecific)]

  • With LINQ query methods:

    [!CODE [Astoria NorthwindClient#LinqQueryExpandMethodSpecific](Astoria NorthwindClient#LinqQueryExpandMethodSpecific)]

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 TypeUnsupported Method
Set operatorsAll set operators are unsupported against a DataServiceQuery<TElement>, which included the following:

- All<TSource>
- Any<TSource>
- Concat<TSource>
- DefaultIfEmpty<TSource>
- Distinct<TSource>
- Except<TSource>
- Intersect<TSource>
- Union<TSource>
- Zip<TFirst, TSecond, TResult>
Ordering operatorsThe following ordering operators that require IComparer<T> are unsupported against a DataServiceQuery<TElement>:

- OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
- OrderByDescending<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
- ThenBy<TSource, TKey>(IOrderedEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
- ThenByDescending<TSource, TKey>(IOrderedEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
Projection and filtering operatorsThe following projection and filtering operators that accept a positional argument are unsupported against a DataServiceQuery<TElement>:

- Join<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<TKey>)
- Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, Int32, TResult>)
- SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TResult>>)
- SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, Int32, IEnumerable<TResult>>)
- SelectMany<TSource, TCollection, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
- SelectMany<TSource, TCollection, TResult>(IEnumerable<TSource>, Func<TSource, Int32, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
- Where<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>)
Grouping operatorsAll grouping operators are unsupported against a DataServiceQuery<TElement>, including the following:

- GroupBy<TSource, TKey>
- GroupJoin<TOuter, TInner, TKey, TResult>

Grouping operations must be performed on the client.
Aggregate operatorsAll aggregate operations are unsupported against a DataServiceQuery<TElement>, including the following:

- Aggregate<TSource>
- Average
- Count<TSource>
- LongCount<TSource>
- Max
- Min
- Sum

Aggregate operations must either be performed on the client or be encapsulated by a service operation.
Paging operatorsThe following paging operators are not supported against a DataServiceQuery<TElement>:

- ElementAt<TSource>
- Last<TSource>
- LastOrDefault<TSource>
- SkipWhile<TSource>
- TakeWhile<TSource> Note: Paging operators that are executed on an empty sequence return null.
Other operatorsThe following other operators are not supported against a DataServiceQuery<TElement>:

1. Empty<TResult>
2. Range
3. Repeat<TResult>
4. ToDictionary<TSource, TKey>
5. ToLookup<TSource, TKey>

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 MemberSupported 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)
Lengthint 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 Member1Supported OData Function
Dayint day(DateTime p0)
Hourint hour(DateTime p0)
Minuteint minute(DateTime p0)
Monthint month(DateTime p0)
Secondint second(DateTime p0)
Yearint 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 MemberSupported 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)
[T: System.Linq.Expressions.Expression](assetId:///T: System.Linq.Expressions.Expression?qualifyHint=False&autoUpgrade=True) MemberSupported 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.

Querying the Data Service
Query Projections
Object Materialization
OData: URI Conventions