← Tutti gli articoli
Compiled Query in the Entity Data Framework
27 August 2012 ·
EDF · Article ·
362 visite
Improve Entity Data Framework performance in ripetitive query
The translation from LINQ to Sql statements requires CPU and time resources. It is possible to compile a LINQ query
- static Func<MaragnaEntities, string, IQueryable<Contents>>
- contenuti =
- CompiledQuery.Compile<MaragnaEntities, string, IQueryable<Contents>>(
- (context, title) =>
- from c in context.Contents
- where c.Title == title
- select c
- );
In this code snippet, the LINQ query is compiled with the help of the Compile method of the CompiledQuery class and stored in a static field called contenuti.
The compiled query itself is a lambda expression, which takes an object context as its first parameter, a title as its second, and returns an IQueryable of Contents.
- using (MaragnaEntities context = new MaragnaEntities())
- {
- var query = contenuti.Invoke(context, "ASP.NET");
- foreach (Contents c in query
- Console.WriteLine(c.ContactName);
- }