title: Implementing conditional 'if' statements within a LINQ queries 'where' statement
body: <p>I'm trying to figure out a way of querying an object in my datamodel and include only those parameters that are not null.  Like below:</p>
  
  <pre><code>public List&lt;Widget&gt; GetWidgets(string cond1, string cond2, string cond3)
  {
      MyDataContext db = new MyDataContext();
      List&lt;Widget&gt; widgets = (from w in db.Widgets
                              where 
                                  ... if cond1 != null w.condition1 == cond1 ...
                                  ... if cond2 != null w.condition2 == cond2 ...
                                  ... if cond3 != null w.condition3 == cond3 ...
                              select w).ToList();
      return widgets;
  }
  </code></pre>
  
  <p>Since the widgets table can get very large, I'd like to avoid doing this:</p>
  
  <pre><code>public List&lt;Widget&gt; GetWidgets(string cond1, string cond2, string cond3)
  {
      MyDataContext db = new MyDataContext();
      List&lt;Widget&gt; widgets = db.Widgets.ToList();
  
      if(cond1 != null)
          widgets = widgets.Where(w =&gt; w.condition1 == cond1).ToList();
  
      if(cond2 != null)
          widgets = widgets.Where(w =&gt; w.condition2 == cond2).ToList();
  
      if(cond3 != null)
          widgets = widgets.Where(w =&gt; w.condition3 == cond3).ToList();
  
      return widgets;
  }
  </code></pre>
  
  <p>I've looked at several example but don't really see anything that matches what I need to do.</p>