title: LINQ: Group By + Where in clause
body: <p>I'm trying to implement a T-SQL equivalent of a <code>where in (select ...)</code> code in LINQ.</p>
  
  <p>This is what I have now:</p>
  
  <pre><code>int contactID = GetContactID();
  IEnumerable&lt;string&gt; threadList = (from s in pdc.Messages
                               where s.ContactID == contactID
                               group 1 by new { s.ThreadID } into d
                               select new { ThreadID = d.Key.ThreadID}).ToList&lt;string&gt;();
  
          var result = from s in pdc.Messages
                       where threadList.Contains(s.ThreadID)
                       group new { s } by new { s.ThreadID } into d
                       let maxMsgID = d.Where(x =&gt; x.s.ContactID != contactID).Max(x =&gt; x.s.MessageID)
                       select new {
                           LastMessage = d.Where(x =&gt; x.s.MessageID == maxMsgID).SingleOrDefault().s
                       };
  </code></pre>
  
  <p>However, my code won't compile due to this error for the <code>ToList()</code>:</p>
  
  <blockquote>
    <p>cannot convert from
    '<code>System.Linq.IQueryable&lt;AnonymousType#1&gt;</code>'
    to
    '<code>System.Collections.Generic.IEnumerable&lt;string&gt;</code>'</p>
  </blockquote>
  
  <p>Anyone have any suggestions on how to implement this? Or any suggestions on how to simplify this code?</p>