I know about the method discussed here:
Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites
... but this doesn't work for my situation as i get a :
"Setting load options is not allowed after results have been returned from a query."
I am using Codesmith PLINQO scripts to generate entities and manager code, and the manager code looks something like this:
public partial class SearchManager
{
#region Query
// A private class for lazy loading static compiled queries.
private static partial class Query
{
internal static readonly Func<MyDataContext,IOrderedQueryable<Search>>
GetAll = CompiledQuery.Compile(
(MyDataContext db) =>
from s in db.Search
orderby s.Name
select s);
}
#endregion
public IQueryable<Search> GetAll()
{
return Query.GetAll(Context);
}
}
I first tried dropping a static DataLoadOptions into the Searchmanager class like this:
public static readonly DataLoadOptions MyOptions =
(new Func<DataLoadOptions>(() =>
{
var option = new DataLoadOptions();
option.LoadWith<Search>(x => x.Rule);
return option;
}))();
... then providing it to the Context in the GetAll method like:
public IQueryable<Search> GetAll()
{
Context.LoadOptions = MyOptions;
return Query.GetAll(Context);
}
...and that gave me the error i noted above. Is this because the query is already compiled, and thus can't have "extra" DataLoadOptions's added? If so, how would it be possible to apply the DataLoadOptions prior to the the query being compiled?
In the setter property of the DataContext class, there is a condition that checks if the DataContext has any objects in its Cache, and the LoadOptions is NOT null, and the LoadOptions instance you are trying to set is not the same as the one already set, then you get that exception.
Alternative #1. Create a new Context for each query (probably not a good idea)
Alternative #2. Call the ClearCache method using reflection, then make a new LoadOptions statically, assign it to the Context, then finally, get the compiled query.