title: Entity Framework sub query
body: <p>How to write sub queries like these in EF?</p>
  
  <pre><code>select * from table1 where col1 in (select col1 from table2 where col2 = 'xyz')
  </code></pre>
  
  <p>or</p>
  
  <pre><code>select * from table1 where col1 not in (select col1 from table2 where col2 = 'xyz')
  </code></pre>
  
  <p>I tried something like these</p>
  
  <pre><code>from t1 in table1
  where (from t2 in table2 where col2 = 'xyz' select t2.col1).Contains(t1.col1)
  select t1
  </code></pre>
  
  <p>and</p>
  
  <pre><code>from t1 in table1
  where !(from t2 in table2 where col2 = 'xyz' select t2.col1).Contains(t1.col1)
  select t1
  </code></pre>
  
  <p>these queries are working fine LinqPad or Linq to Sql</p>