title: LINQ: When to use Compiled Queries?
body: <p>I'd like some expert advice on this. I've used compiled queries before, but for this particular case, i'm not sure whether it's appropriate.</p>
  
  <p>It's a search form where the query changes and is dependent on what is being searched on. </p>
  
  <pre><code>static Func&lt;DBContext, int, IQueryable&lt;Foo&gt;&gt; Search = CompiledQuery.Compile(
      (DBContext db, int ID) =&gt;
          db.Person
              .Where(w =&gt; w.LocationID = ID)
              .Select(s =&gt; 
                  new Foo 
                  { 
                      Name = s.PersonName, 
                      Age = s.Age,
                      Location = s.LocationName,
                      Kin = s.Kin
                  }));
  </code></pre>
  
  <p>Now if someone fills in the search box, i want to extend the query by adding another <code>Where</code> statement to the query:</p>
  
  <pre><code>var query = Search(context, 123);
  query = query.Where(w =&gt; w.Name.Contains(searchString));
  </code></pre>
  
  <p>So my question is, is it returning all the results where <code>LocationID == 123</code>, then checking the results for a <code>searchString</code> match? Or is it actually extending the compiled query?</p>
  
  <p>If it's the former (which i suspect it is), should scrap the <code>CompiledQuery</code> and just create a method that extends the query then return it as a list?</p>
  
  <p>Also, what are the best practices for <code>CompiledQuery</code> usage and is there a guideline of when they should be used? </p>
  
  <p>Note: I'm using the above in an <strong>ASP.NET</strong> website with <strong>Linq to SQL</strong>. Not sure if that makes any difference.</p>
  
  <p>Thanks</p>