title: Dynamically Generating a Linq/Lambda Where Clause
body: <p>I've been searching here and Google, but I'm at a loss. I need to let users search a database for reports using a form. If a field on the form has a value, the app will get any reports with that field set to that value. If a field on a form is left blank, the app will ignore it. How can I do this? Ideally, I'd like to just write Where clauses as Strings and add together those that are not empty.</p>
  
  <p><code>.Where("Id=1")</code></p>
  
  <p>I've heard this is supposed to work, but I keep getting an error: "could not be resolved in the current scope of context Make sure all referenced variables are in scope...".</p>
  
  <p>Another approach is to pull all the reports then filter it one where clause at a time. I'm hesitant to do this because 1. that's a huge chunk of data over the network and 2. that's a lot of processing on the user side. I'd like to take advantage of the server's processing capabilities. I've heard that it won't query until it's actually requested. So doing something like this</p>
  
  <pre><code>    var qry = ctx.Reports
            .Select(r =&gt; r);
  </code></pre>
  
  <p>does not actually run the query until I do:</p>
  
  <pre><code>    qry.First()
  </code></pre>
  
  <p>But if I start doing:</p>
  
  <pre><code>    qry = qry.Where(r =&gt; r.Id = 1).Select(r =&gt; r);
      qry = qry.Where(r =&gt; r.reportDate = '2010/02/02').Select(r =&gt; r);
  </code></pre>
  
  <p>Would that run the query? Since I'm adding a where clause to it. I'd like a simple solution...in the worst case I'd use the Query Builder things...but I'd rather avoid that (seems complex).</p>
  
  <p>Any advice? :)</p>