title: Problem with slow query using predicate builder
body: <p>This question takes no time at all to execute, but if I uncomment the last line it takes 5 seconds.  How could this affect the query that much? It's almost the exact same query.  </p>
  
  <pre><code>return db.Parties.Where(predicate).Select(p =&gt; new SearchResult
  {
      adressPerson = p.Contacts.SingleOrDefault(m =&gt; m.contact_type == "H").streetname,
      //adressOrg = p.Contacts.SingleOrDefault(m =&gt; m.contact_type == "W").streetname
  });  
  </code></pre>
  
  <p>How do I locate the problem?  </p>
  
  <p>Here's what the predicate looks like:  </p>
  
  <pre><code>    predicate = predicate.Or(p =&gt; p.surname.EndsWith(keyword));
      predicate = predicate.Or(p =&gt; p.lastname.EndsWith(keyword));
      predicate = predicate.Or(p =&gt; p.organisation.EndsWith(keyword));
      predicate = predicate.Or(p =&gt; p.Contacts.Any(c =&gt; c.streetname.EndsWith(keyword)));
      predicate = predicate.Or(p =&gt; p.Memberships.Any(m =&gt; m.Congregation.congregation_name.EndsWith(keyword)));
      predicate = predicate.Or(p =&gt; p.PartyCategories.Any(m =&gt; m.Category.category_name.EndsWith(keyword) || m.Category.category_code.EndsWith(keyword)));  
  </code></pre>
  
  <p><em>I know it's stupid to post a lot of code but here's the SQL-query anyway. Any clues?</em> </p>
  
  <pre><code>SELECT (
      SELECT [t6].[streetname]
      FROM [dbo].[Contact] AS [t6]
      WHERE ([t6].[contact_type] = @p7) AND ([t6].[party_id] = [t0].[party_id])
      ) AS [adressPerson], (
      //SELECT [t7].[streetname]
      //FROM [dbo].[Contact] AS [t7]
      //WHERE ([t7].[contact_type] = @p8) AND ([t7].[party_id] = [t0].[party_id])
      //) AS [adressOrg]
  FROM [dbo].[Party] AS [t0]
  WHERE ([t0].[surname] LIKE @p0) OR ([t0].[lastname] LIKE @p1) OR ([t0].[organisation] LIKE @p2) OR (EXISTS(
      SELECT NULL AS [EMPTY]
      FROM [dbo].[Contact] AS [t1]
      WHERE ([t1].[streetname] LIKE @p3) AND ([t1].[party_id] = [t0].[party_id])
      )) OR (EXISTS(
      SELECT NULL AS [EMPTY]
      FROM [dbo].[Membership] AS [t2]
      LEFT OUTER JOIN [dbo].[Congregation] AS [t3] ON [t3].[congregation_id] = [t2].[congregation_id]
      WHERE ([t3].[congregation_name] LIKE @p4) AND ([t2].[party_id] = [t0].[party_id])
      )) OR (EXISTS(
      SELECT NULL AS [EMPTY]
      FROM [dbo].[PartyCategories] AS [t4]
      INNER JOIN [dbo].[Category] AS [t5] ON [t5].[category_id] = [t4].[category_id]
      WHERE (([t5].[category_name] LIKE @p5) OR ([t5].[category_code] LIKE @p6)) AND ([t4].[party_id] = [t0].[party_id])
  ))
  </code></pre>
  
  <p><strong>SOLVED</strong>
  Turned out that the INNER JOIN in the last select statement didn't work. So I changed the last line of the predicate to:  </p>
  
  <pre><code>predicate = predicate.Or(p =&gt; p.PartyCategories
      .GroupJoin(db.Categories, b =&gt; b.category_id, c =&gt; c.category_id, (b, c) =&gt; new { b, c })
      .SelectMany(c =&gt; c.c.DefaultIfEmpty(), (c, b) =&gt; new { c.c, b })
      .Any(m =&gt; m.b.category_name.EndsWith(keyword) || m.b.category_code.EndsWith(keyword)));
  </code></pre>