Is there a way to eliminate the repetition in this linq query? This is a query to return search results, but I feel it could be cleaned up a little.  I just don't know how.  

var queryResult = (from r in dc.Retailers
                    where
                        (string.IsNullOrEmpty(firstName) ||
                        SqlFunctions.PatIndex(firstName.Trim(), r.FirstName.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(lastName) ||
                        SqlFunctions.PatIndex(lastName.Trim(), r.LastName.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(companyName) ||
                        SqlFunctions.PatIndex(companyName.Trim(), r.CompanyName.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(phone) ||
                        SqlFunctions.PatIndex(phone.Trim(), r.Phone.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(email) ||
                        SqlFunctions.PatIndex(email.Trim(), r.Email.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(city) ||
                        SqlFunctions.PatIndex(city.Trim(), r.City.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(zip) ||
                        SqlFunctions.PatIndex(zip.Trim(), r.Zip.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(state) ||
                        SqlFunctions.PatIndex(state.Trim(), r.State.Trim()) &gt; 0) &amp;&amp;
                        (string.IsNullOrEmpty(country) ||
                        SqlFunctions.PatIndex(country.Trim(), r.Country.Trim()) &gt; 0)
                    select r
    );


Update: For those confused by the query.  Here is another version, which is probably better because it only builds the where parts when necessary.  But I would still like to reduce the repetition.  

var queryResult = (from r in dc.Retailers select r);
if (!string.IsNullOrEmpty(firstName))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(firstName.Trim(), ex.FirstName.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(lastName))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(lastName.Trim(), ex.LastName.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(companyName))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(companyName.Trim(), ex.CompanyName.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(phone))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(phone.Trim(), ex.Phone.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(email))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(email.Trim(), ex.Email.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(city))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(city.Trim(), ex.City.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(zip))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(zip.Trim(), ex.Zip.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(country))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(country.Trim(), ex.Country.Trim()) &gt; 0);
if (!string.IsNullOrEmpty(state))
    queryResult = queryResult.Where(ex =&gt; SqlFunctions.PatIndex(state.Trim(), ex.State.Trim()) &gt; 0);
Apply the common refactoring used for repeated code: extracting a method. In your case, creating the method won't be trivial, because you want to create a different Expression each time. That is, unless you use LINQKit:

public static IQueryable&lt;T&gt; WhereEqualsByPatIndex&lt;T&gt;(
    this IQueryable&lt;T&gt; query, string value, Expression&lt;Func&lt;T, string&gt;&gt; propertyExpression)
{
    if (string.IsNullOrEmpty(value))
        return query;

   return query.Where(
       ex =&gt; SqlFunctions.PatIndex(
           value.Trim(), propertyExpression.Invoke(ex, propertyExpression).Trim()) &gt; 0);
}


Usage would be like this (that call to AsExpandable() is important):

dc.Retailers
  .AsExpandable()
  .WhereEqualsByPatIndex(firstName, ex =&gt; ex.FirstName)
  .WhereEqualsByPatIndex(lastName, ex =&gt; ex.LastName)