O'Reilly logo

WCF 4.0 Multi-tier Services Development with LINQ to Entities by Mike Liu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Compiled query

It is common in many applications to execute structurally similar queries many times. In such cases it is possible to increase performance by compiling the query once and executing it several times in the application with different parameters. This result is obtained in LINQ to Entities by using the CompiledQuery class.

The following code shows how to define a compiled query:

static void TestCompiledQuery() { NorthwindEntities NWEntities = new NorthwindEntities(); Func<NorthwindEntities, string, IQueryable<Product>> fn = CompiledQuery.Compile((NorthwindEntities NW, string category) => from p in NW.Products where p.Category.CategoryName == category select p); var products1 = fn(NWEntities, '"Beverages'"); Console.WriteLine('"Total ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required