views:

307

answers:

2

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?

A: 

The error message itself tells you exactly what is wrong. You cannot apply DataLoadOptions after a Linq query has returned results. Or, maybe a better way to say this is as follows. If you want to apply DataLoadOptions, do so before executing the query. You cannot do so afterwards.

Randy Minder
Actually the error occurs before the query is called, as shown : public IQueryable<Search> GetAll() { Context.LoadOptions = MyOptions; return Query.GetAll(Context); }
krisg
A: 

You can only set the load options once for a compiled query. The error must be getting thrown on the second call. Move the assignment to a static constructor and that should solve your problem.

Bryce Kahle