Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Is your reference to the context static, i.e. you've got a single context across the type? That doesn't sound like a great idea to me. Anyway, leaving that to one side, you can do:</p> <pre><code>// Private version which takes a context... private static Func&lt;LINQDBDataContext, string, IQueryable&lt;Pet&gt;&gt; QueryFindByNameImpl = CompiledQuery.Compile(( LINQDBDataContext context, string name) =&gt; from p in context.Pets where p.Name == name select p); // Public version which calls the private one, passing in the known context public static Func&lt;string, IQueryable&lt;Pet&gt;&gt; QueryFindByName = name =&gt; QueryFindByNameImpl(contextFromType, name); </code></pre> <p>EDIT: Okay, if you don't like this approach, you could try writing your own generic wrappers around <code>CompiledQuery.Compile</code> instead. For example:</p> <pre><code>public static class LinqHelpers { public static Func&lt;TArg0, TResult&gt; Compile&lt;TContext, TArg0, TResult&gt; (this TContext context, Expression&lt;Func&lt;TContext, TArg0, TResult&gt;&gt; query) where TContext : DataContext { Func&lt;TContext, TArg0, TResult&gt; compiled = CompiledQuery.Compile(query); return arg =&gt; compiled(context, arg); } } </code></pre> <p>(And so on for more parameters.)</p> <p>I haven't tried even compiling that, but I <em>think</em> it will work. You'd then use it like this:</p> <pre><code>private static MyLinqDataContext context = SomeUtilityClass.GetMeMyContext(); public static Func&lt;string, IQueryable&lt;Pet&gt;&gt; QueryFindByName = context.Compile ((LINQDBDataContext context, string name) =&gt; from p in context.Pets where p.Name == name select p); </code></pre> <p>It's still creating a wrapper of course, but at least you only need to do it in one place. If your objection to creating wrappers was anything other than the tedium/code mess of it, please give more details.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload