There may be scenario where we need to execute a particular query many times and repeatedly. LINQ allows us to make this task very easy by enabling us to create a query and make it compiled always. Benefits of Compiled Queries:
- Query does need to compiled each time so execution of the query is fast.
- Query is compiled once and can be used any number of times.
- Query does need to be recompiled even if the parameter of the query is being changed.
Consider:
static class MyCompliedQueries {
public static Func <DataClasses1DataContext, IQueryable <Person>> CompliedQueryForPerson =
CompiledQuery.Compile((DataClasses1DataContext context) = >from c in context.Persons select c);
}