title: Build Dynamic queries with LINQ
body: <p>I was trying to build a dynamic generic method. To create a advanced search mechanism.</p>
  
  <p>I could achieve something using Dynamic LINQ</p>
  
  <pre><code>IQueryable&lt;Table&gt; query = ObjectContext.Table;
  if (parameters != null &amp;&amp; parameters.Count &gt; 0)
  {
      foreach (KeyValuePair&lt;string, dynamic&gt; keyValuePair in parameters)
      {
          query = query.Where(keyValuePair.Key + " == @0", new object[] { keyValuePair.Value });
      }
  }
  </code></pre>
  
  <p>But doing this, I need to load each field with something like this</p>
  
  <pre><code>ClassTable.Parameters.Add("FKTable.Foo", foo);
  ClassTable.Parameters.Add("Bar", bar);
  </code></pre>
  
  <p>So I'm trying with other approach <strong><em>(This code works)</em></strong></p>
  
  <pre><code>List&lt;Table&gt; lstTable = new List&lt;Table&gt;();
  lstTable.AddRange(tableDAO.SelectWhere(
      u =&gt; this.EntityValues.Foo == u.Foo &amp;&amp; this.EntityValues.Bar == u.Bar
  ));
  return lstTable;
  </code></pre>
  
  <p>Now, my problem is, I wanted to do something more like <strong><em>(This code brings the result of the first query only)</em></strong></p>
  
  <pre><code>List&lt;Table&gt; lstTable = new List&lt;Table&gt;();
  lstTable.AddRange(tableDAO.SelectWhere(
      u =&gt; !string.IsNullOrEmpty(this.EntityValues.Foo) ? this.EntityValues.Foo == u.Foo : false 
      &amp;&amp;
      this.EntityValues.Bar != 0 ? this.EntityValues.Bar == u.Bar : false
  ));
  return lstTable;
  </code></pre>
  
  <p>I didn't want to do something like this</p>
  
  <pre><code>IQueryable&lt;Data.Story&gt; query = ctx.DataContext.Stories;
  
  if (criteria.StoryId != null) // StoryId
      query = query.Where(row =&gt; row.StoryId == criteria.StoryId);
  
  if (criteria.CustomerId != null) // CustomerId
      query = query.Where(row =&gt; row.Project.CustomerId == criteria.CustomerId);
  
  if (criteria.SortBy != null) // SortBy
      query = query.OrderBy(criteria.SortBy + " " + criteria.SortOrder.Value.ToStringForSql());
  </code></pre>
  
  <p>I know that my question is kinda jumbled, I'll provide edits and comments to fix it. just let me know.</p>
  
  <hr>
  
  <p><strong>TL;DR;</strong> I need help to create a dynamic query, where I only need to pass the parameters that are used in the search. So I can create a advanced search option to the user.</p>