Can I use a compiled query in a web service?

I have a WCF service with a select method requiring only an id. I need to use several (20+) load options to get everything I need to return so I would think this make a pretty complicated query and compiling it would be a good idea. the only way I have found so far is to create a static data context and load options to use with the compiled query. If I don't it complains about using a different context than it was compiled with. So that makes my question should I do this? Will having a static data context on a web service cause issues? It would only be used for this method. All other methods create their own. I'm just afraid I might end up paying for this decision down the road so any tips would be great! Thanks!

    private static DataLoadOptions loadOptions = GetDataLoadOpt();

    private static readonly Func<ItemsDataContext, int, Data.Item> SelectItem =
        CompiledQuery.Compile((ItemsDataContext DataContext, int ItemId) =>
            DataContext.Items.Where(r => r.ItemID == ItemId).FirstOrDefault());

    private static DataLoadOptions GetDataLoadOpt()
        var loadOpts = new System.Data.Linq.DataLoadOptions();
        loadOpts.LoadWith<Data.Item>(d => d.ItemProp);
        return loadOpts;


Web service calls are distinct. I haven't found that having a shared, static context saves any time because the call ends up spinning up a new instance anyway. And I also have several objects that have very large, complicated loadoptions stacks in a few of my projects. In some cases, I have a bit of performance drop because I'm bringing back so many tables, but if the joins are strong (well-designed DB), then it's not too bad.

Need Your Help

Insert object into a specified index in an ArrayList

java arraylist

Let's say I have a class called myClass&lt;K,V&gt;.

Visual Studio Text Box Wrap Function

visual-studio textbox wrapping

How can I make my text wrap automatically to the next line when I am writing text in a textbox rather than keeps on typing in one line?