title: ASP.NET C# LINQ conditional WHERE clause
body: <p>I'm trying to get values from numerous dropdown lists and build a where statement depending on the options selected. If they are not slected then they should be excluded from the select statement.</p>
  
  <p>This is how I would have done it but I gather that it can not be done in this way with linq.</p>
  
  <pre><code> IEnumerable&lt;IGrouping&lt;string, Forest&gt;&gt; treeQuery =
  
          from trees in Forest
          if (ddlType1.SelectedValue!=null)
          {
              string strWhere += trees.Type1 == ddlType1.SelectedValue
          }
          else if (ddlType2.SelectedValue!=null)
          {
              string strWhere += trees.Type2 == ddlType2.SelectedValue
          }
          where strWhere
          orderby trees.Nuts
          group trees by trees.TrunkColour;
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>This is the code before I added the example in...</p>
  
  <pre><code> IEnumerable&lt;IGrouping&lt;string, Forest&gt;&gt; treeQuery =
  
          from trees in Forest
          where trees.Type1 == "oak"
          orderby trees.Nuts
          group trees by trees.TrunkColour; 
  </code></pre>