What are the different steps involved to write compiled LINQ queries?

 Posted by ArticlesMaint on 9/29/2009 | Category: LINQ Interview questions | Views: 4726

The first thing is to import Data.Linq namespace.

Import namespace using System.Data.Linq;


The syntax to write compiled queries is a bit cryptic. So let us break those syntaxes in small pieces and then we will try to see how the complete syntax looks like. To execute a compiled function we need to write function to pointer. This function should be static so that LINQ engine can use the query plan stored in those static class objects.
Below is how we define the function it starts with ‘public static’ stating that this function is static. Then we use the ‘Func’ keyword to define the input parameters and output parameters. Below is how the parameter sequence needs to be defined:-
• The first parameter should be a data context. So we have defined the data type as ‘DataContext’.
• Followed by 1 or many input parameters currently we have only one i.e. customer code so we have defined the second parameter data type as string.
• Once we are done with all input parameters we need to define the data type of the output. Currently we have defined the output data type as ‘IQueryable’.
We have given a name to this delegate function as ‘getCustomers’.

public static Func<DataContext, string, IQueryable<clsCustomerEntity>> getCustomers


We need to call method ‘Compiled’ of static class ‘CompiledQuery’ with the datacontext object and necessary define input parameters followed by the LINQ query. For the below snippet we have not specified the LINQ query to minimize complications.

CompiledQuery.Compile((DataContext db, string strCustCode)=> Your LINQ Query );


So now uniting the above two code snippets below is how the complete code snippet looks like.

public static Func<DataContext, string, IQueryable<clsCustomerEntity>> getCustomers= CompiledQuery.Compile((DataContext db, string strCustCode)=> Your LINQ Query );


We then need to wrap this static function in a static class. So we have taken the above defined function and wrapped that function in a static class ‘clsCompiledQuery’.

public static class clsCompiledQuery

public static Func<DataContext, string, IQueryable<clsCustomerEntity>>
getCustomers = CompiledQuery.Compile((DataContext db, string strCustCode)
=> from objCustomer in db.GetTable<clsCustomerEntity>()
where objCustomer.CustomerCode == strCustCode
select objCustomer);



Consuming the compiled query is pretty simple; we just call the static function. Currently this function is returning data type as ‘IEnumerable’. So we have to define an ‘IEnumerable’ customer entity which will be flourished through the ‘getCustomers’ delegate function. We can loop through the customer entity using ‘clsCustomerEntity’ class.

IQueryable<clsCustomerEntity> objCustomers = clsCompiledQuery.getCustomers(objContext, txtCustomerCode.Text);
foreach (clsCustomerEntity objCustomer in objCustomers)
Response.Write(objCustomer.CustomerName + "<br>");

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response