Click here to Skip to main content
12,821,494 members (23,465 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 19 Oct 2011

Increase LINQ query perfromance by compiling it

, 20 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Increase LINQ query perfromance by compiling it
Each time when we fire any Linq to SQL or to any other data-source using Entity Framework query run-time convert query to expression tree and then into t-SQL statement. So if the query gets fired a number of times in the application, it gets converted in the expression tree to t-SQL statement by run-time this conversion increases execution time which in turn affects performance of the application. To avoid this cost, Microsoft .NET framework introduces the concept of the compiled queries which allows compilation and caching of queries for reuse.

Now, there is a shopping website which list of the product by the category basically it allows filtering of product by the category. So if I have 100 number of users who logged in to system and do the filter the product by category they basically firequery to get the result they want. So this will increase the execution cost as this query gets fired a number of times and gets converted in the expression tree and in turn gets the result.

from p in db.Products where p.Category == category select p

With the help of CompiledQuery class of .NET framework, I can rewrite my code and it's like below:

public static Func<dataloadtestdatacontext,>>
 ProductByCategory =
    CompiledQuery.Compile((DataLoadTestDataContext db, string category) =>
     from p in db.Products where p.Category == category select p);

Static Variable
Static variable is used to store, so it's not thread safe and global to all. Because of static variable, compilation will only occur once per AppDomain and will remain cached through the rest of the application's lifetime. If you don't use the static variable, the query gets complied each time which increases the cost and decreases the performance of the application.

Constrain and Use
Cannot use to store the queries which returns Anonymous type, because the anonymous type doesn't have any type to assign generic argument of function.

Useful when query is used heavily and you want to reuse the query, by using this way increase the performance of the application.

Where to include the code ?
Better place to include the above code is partial class, its extended partial class to the partial class generated by ORM tool.
More: Extended ORM generated class[^]

Reference from Linq and C#.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 2 Not really any extra info besides wh... Pin
Reonekot25-Oct-11 1:35
memberReonekot25-Oct-11 1:35 
QuestionHow to use? Pin
Klaus Luedenscheidt20-Oct-11 20:01
memberKlaus Luedenscheidt20-Oct-11 20:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 20 Oct 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid