title: Dynamic query with OR conditions in Entity Framework
body: <p>I am creating an application that searches the database and allows the user to  dynamically add any criteria (around 50 possible), much like the following SO question: <a href="https://stackoverflow.com/q/5541234/810850">Creating dynamic queries with entity framework</a>. I currently have working a search that checks each criteria, and if it is not blank it adds it to the query.</p>
  <p><strong>C#</strong></p>
  <pre><code>var query = Db.Names.AsQueryable();
    if (!string.IsNullOrWhiteSpace(first))
        query = query.Where(q =&gt; q.first.Contains(first));
    if (!string.IsNullOrWhiteSpace(last))
        query = query.Where(q =&gt; q.last.Contains(last));
    //.. around 50 additional criteria
    return query.ToList();
  </code></pre>
  <p>This code produces something similar to the following in sql server (I simplified for easier understanding)</p>
  <p><strong>SQL</strong></p>
  <pre><code>SELECT
      [Id],
      [FirstName],
      [LastName],
      ...etc
  FROM [dbo].[Names]
  WHERE [FirstName] LIKE '%first%'
    AND [LastName] LIKE '%last%'
  </code></pre>
  <p>I am now trying to add a way to generate the following SQL with C# through entity framework but with an <strong>OR</strong> instead of an <strong>AND</strong>, while still keeping the ability to add criteria dynamically.</p>
  <p><strong>SQL</strong></p>
  <pre><code>SELECT
      [Id],
      [FirstName],
      [LastName],
      ...etc
    FROM [dbo].[Names]
  WHERE [FirstName] LIKE '%first%'
    OR [LastName] LIKE '%last%' &lt;-- NOTICE THE &quot;OR&quot;
  </code></pre>
  <p>Usually the criteria wont be larger than two or three items for a query but combining them into one gigantic query is not an option. I have tried concat, union, and intersect and they just all duplicate the query and join them with UNION.</p>
  <p>Is there a simple and clean way to add &quot;OR&quot; conditions to a dynamically generated query using entity framework?</p>
  <p><strong>Edit with my solution - 9/29/2015</strong></p>
  <p>Since posting this, I have noticed this has received a little attention, so I decided to post my solution</p>
  <pre><code>// Make sure to add required nuget
  // PM&gt; Install-Package LinqKit
  
  var searchCriteria = new 
  {
      FirstName = &quot;sha&quot;,
      LastName = &quot;hill&quot;,
      Address = string.Empty,
      Dob = (DateTime?)new DateTime(1970, 1, 1),
      MaritalStatus = &quot;S&quot;,
      HireDate = (DateTime?)null,
      LoginId = string.Empty,
  };
  
  var predicate = PredicateBuilder.False&lt;Person&gt;();
  if (!string.IsNullOrWhiteSpace(searchCriteria.FirstName))
  {
      predicate = predicate.Or(p =&gt; p.FirstName.Contains(searchCriteria.FirstName));
  }
  
  if (!string.IsNullOrWhiteSpace(searchCriteria.LastName))
  {
      predicate = predicate.Or(p =&gt; p.LastName.Contains(searchCriteria.LastName));
  }
  
  // Quite a few more conditions...
  
  foreach(var person in this.Persons.Where(predicate.Compile()))
  {
      Console.WriteLine(&quot;First: {0} Last: {1}&quot;, person.FirstName, person.LastName);
  }
  </code></pre>