A2Zmenu.Com
Blog
Tutorials
Online Exam
Ask Question
Utility
Fun @ Work
Wiki
Contact Us
About Us
Loading
Home
Blogs
AJAX
ASP.NET
BI
CSharp
JavaScript
LINQ
Miscellaneous
MVC
SQL
SharePoint
Silverlight
WCF
Tutorials
HTML5
ASP.NET 4.0
CSharp
SharePoint
SQL Function
Online Exam
ASP.NET
LINQ
Silverlight
SQL
JavaScript
SharePoint
Forum
Utility
Fun @ Work
Appraisal
Funny Images
Funny Puzzle
Miscellaneous
Motivating Stories
Life Vs Work
Wiki
Contact Us
AJAX
ASP.NET
BI
CSharp
JavaScript
LINQ
Miscellaneous
MVC
SQL
SharePoint
Silverlight
WCF
Test Your Skill
Q. Out of Boxing and Unboxing which one is implicit?
Boxing
Unboxing
None
View all questions...
LINQ to SQL CompiledQuery
In the earlier posts on LINQ to SQL performance optimization, I discussed about the
LINQ Best Practices
and LINQ
DataLoadOptions
. This discussion will remain incomplete if I don't include
CompiledQuery.Compile
.
While developing the application we often come across scenario where we are required to execute a similar nature of query many times.
When we execute a query using LINQ there are number of steps involved before final SQL query is generated, and each time we call the method to fetch data whole process starting from query generation to its execution is carried out. In this life cycle the overhead that is easily visible is that we are every time generating the query.
LINQ's CompiledQuery.Compile is used to avoid this extra overhead. If you have an application executing structurally similar query, CompiledQuery.Compile is one thing that can help you increase the performance of your application.
CompiledQuery.Compile compiles the query once and execute it for the rest of the time with different set of parameters.
// Method in which whole process of query generation and execution take place
every time
private
static
void
CompiledQueryMethod(
string
orderID)
{
SampleDataContextDataContext db =
new
SampleDataContextDataContext
();
var result =
from
c
in
db.Customers
join o
in
db.Orders
on
c.CustomerID
equals
o.CustomerID
where
o.OrderID == orderID
select
new
{ c };
}
In this case whole process of query generation to execution is carried out each time CompiledQuery method is called.
Now lets do the same using CompiledQuery.Compile way.
// This will compile the query and return a delegate that can be used to execute for different
// set of parameters
public
static
Func
<
SampleDataContextDataContext
,
int
,
IQueryable
<
Customer
>>
GenerateCompiledQuery =
CompiledQuery
.Compile((
SampleDataContextDataContext
db,
int
orderID) =>
from c
in
db.Customers
join o
in
db.Orders
on
c.CustomerID
equals
o.CustomerID
where o.OrderID == orderID
select c);
The above method returns a delegate that can be cached and used several times just by changing the input parameter.
private
static
void
CompiledQueryMethod(
int
orderID)
{
SampleDataContextDataContext db =
new
SampleDataContextDataContext
();
var result = GenerateCompiledQuery(db,orderID);
}
Related Posts:
LINQ to SQL Best Practice
Using let Keyword in LINQ
Error: Subquery returned more than 1 value
Maintain Transaction in LINQ to SQL
Error: String must be exactly one character long
Perform Cross Database LINQ join Operation
Can't perform Create, Update or Delete operations on Table(Employee) because it has no primary key
Implementing Left Join with LINQ
Comments
Israel Ocbina
Posted on:
9/1/2012 11:17:31 AM
Very nice and simple sample coding.. It help me clear my mind from preventing using linq to sql but now i'm encourage to use it cause of the CompiledQuery method.. Thanks!
Add Comment
Comment
Name
(Required)
E-mail
(Privacy assured)
(Required)
Subscribe to this site by email
Poll of the Day
Q.
Best Smart Mobile Phone in market?
I-Phone
Samsung
Nokia
Others
Related Contents
Online Test Papers
ASP.NET
JavaScript
LINQ
SharePoint
Silverlight
SQL
Related Tutorials
HTML 5 Tutorial
SharePoint Best Practice
How to add ribbon button in SharePoint list and library
List Join operation in SharePoint 2007 / 2010
ASP.NET 4.0 Tutorials
Fun @ Work
Keep smiling always
Developer Vs Tester
There are solutions, even to the hardest problems
Corporate Cartoons
Golden words of steve jobs
Top Blogs
SharePoint 2010 Modal Dialog
How to call webservice using Ajax or JavaScript
Microsoft OLE DB Provider for ODBC Drivers error 80004005
Using SQL SWITCH CASE Statement
Validation of viewstate MAC failed. If this application is hosted...
Creating a Windows Service in C#
Using InstallUtil to install/uninstall service
Each GROUP BY expression must contain at least one column that is not an outer reference
How to add ribbon button in SharePoint list and library
Client side onselectedindexchanged or onchange event in RadioButtonList
Arithmetic overflow error
Pass Data from Parent window to ChildWindow
Silverlight: ListBox with CheckBox
Tablix headers not repeating in SSRS 2008
LINQ to SQL Best Practice
Calling JavaScript function from C#
The contract name could not be found in the list of contracts implemented by the service
Like To Connect With US
x