title: How to Query Exists and Not Exists from Same Table, Using Linq to Entities
body: <p>I have a table that has a list of PartIds and AccountIds associated with those parts. In that table, there is a boolean that determines whether the PartId/AccountId pairing are a member of an exclusive club (IsInExclusiveClub).</p>
  
  <p>I want to query the table to find the distinct PartId/AccountId pairing for both belonging to the exclusive club and not belonging to the exclusive club.</p>
  
  <p>Here are the two SQL queries that I have written to do this:</p>
  
  <p><strong>For being IN the exclusive club:</strong></p>
  
  <pre><code>DECLARE                           @ForeignPassedInKey int; set @ForeignPassedInKey = [Some Integer]
  
  SELECT                     DISTINCT p.PartId
  FROM                       dbo.Part p
  WHERE                      IsInExclusiveClub = 1
  AND                        EXISTS(SELECT * FROM dbo.Parts p2 
                                    WHERE p.partid = p2.partid 
                                    AND p2.Account IN (SELECT AccountId FROM dbo.Accounts
                                                                 WHERE ForeignPassedInKey = @ForeignPassedInKey)
                                    AND p2.IsInExclusiveClub = 1)
  </code></pre>
  
  <p><strong>For being excluded from the exclusive club:</strong></p>
  
  <pre><code>DECLARE                           @ForeignPassedInKey int; set @ForeignPassedInKey = [Some Integer]
  
  SELECT                     DISTINCT p.PartId
  FROM                       dbo.Part p
  WHERE                      IsInExclusiveClub = 1
  AND                        NOT EXISTS(SELECT * FROM dbo.Parts p2 
                                    WHERE p.partid = p2.partid 
                                    AND p2.Account IN (SELECT AccountId FROM dbo.Accounts
                                                                 WHERE ForeignPassedInKey = @ForeignPassedInKey)
                                    AND p2.IsInExclusiveClub = 1)
  </code></pre>
  
  <p>How would I write that using Linq to Entities. Also, let me know if you need any more information.</p>