How to: Detect Client-Side LINQ Queries

The Telerik Data Access Profiler and Tuning Advisor detects client-side LINQ queries.

Some of the LINQ queries executed via the OpenAccessContext API are not completely executed on the database server, i.e. they are not completely translated to SQL, but part of them is executed on the client-side using Linq to Objects. These queries do return the correct results to the user, but usually there is a certain performance penalty during their execution. The Telerik Data Access Profiler detects such queries and brings an alert, which shows the user which query is partially executed on the client, and which part is executed on the database server.

For example, in the following query, the user tries to execute the Union operation on the Customer records whose company name starts with “A” and the ones whose company name starts with “B”.

var items = dbContext.Customers.
   Where( x => x.CompanyName.StartsWith( "A" ) ).
   Select( x => x ).
   Union( dbContext.Customers.Where( x => x.ContactName.StartsWith( "B" ) ).
   Select( x => x ) );
Dim items = dbContext.Customers.
    Where(Function(x) x.CompanyName.StartsWith("A")).
    Select(Function(x) x).
    Union(dbContext.Customers.Where(Function(x) x.ContactName.StartsWith("B")).
    Select(Function(x) x))

The Union operation can’t be executed on the database server and it is executed on the client. This is caught by the Telerik Data Access Profiler and the following alert will be shown:

The part of the query that is executed on the server is Bold, so you can see where the query is actually "cut" to a server-side piece and a client-side piece.

How to: Avoid Client-Side LINQ Queries

To avoid client-side LINQ queries, you should re-write the query. For example, the previous sample query should be re-written in the following way:

var items = dbContext.Customers.
   Where( x => x.CompanyName.StartsWith( "A" ) || x.CompanyName.StartsWith( "B" ) ).
   Select( x => x );
Dim items = dbContext.Customers.
    Where(Function(x) x.CompanyName.StartsWith("A") OrElse x.CompanyName.StartsWith("B")).
    Select(Function(x) x)

This time, the query will return the same result but it will be completely executed on the database server.

How to Enable/Disable the Client-Side Linq Query Alert

You could edit the alert settings for the "client-side Linq query" problem in the Profiler Settings dialog. Open the Profiler Settings dialog and go to the Alert Rules.