title: Dynamically Build Linq Lambda Expression
body: <p>Okay, my guess is this is answered somewhere already, and I'm just not quite familiar enough with the syntax yet to understand, so bear with me.</p>
  
  <p>The users of my web app need to filter a long list of items in a gridview, accessed via a linqdatasource.  I am using the OnSelecting Event to further filter items.  I want to filter those items based on selections the users make in DropDownLists.</p>
  
  <p>For example, they select "Title" "Contains" "Fred"
  This results in</p>
  
  <pre><code>e.Result = dbContext.Opps.Where(opp =&gt; opp.Title.Contains("Fred"));
  </code></pre>
  
  <p>Or "Description" "Does not Contain" "Alpha"
  results in</p>
  
  <pre><code> e.Result = dbContext.Opps.Where(opp =&gt; !opp.Description.Contains("Alpha"));
  </code></pre>
  
  <p>I would like to build up that Expression (System.Linq.Expressions.Expression>) dynamically, instead of having nested switch expressions to generate it, as there are a number of fields I want to check, and I also want to use the StartsWith and EndsWith checks.  If I could build the Expression as a string, like so:</p>
  
  <pre><code>string stringExpression = string.Format("opp =&gt; opp.{0}.{1}(\"{2}\")",
      ddlCustomFilter.SelectedValue,
      ddlFilterType.SelectedValue,
      txtFilterText.Text);
  </code></pre>
  
  <p>And then somehow have it be converted into an Expression...  is this possible?  Or should I just bite the bullet and generate all the switch() statements required to create the various expressions?</p>