title: Entity Framework .Where nested in .Include
body: <p>I'm attempting to perform a db lookup using EF5 code-first. The basic structure and table relationships are as follows;</p>
  
  <pre><code>public partial class Member
  {
      public int    RecordID {get; set;}
      public string Name     {get; set;}
      ...etc.
      public virtual ICollection&lt;MemberLink&gt; MasterLinks {get; set;}
      public virtual ICollection&lt;MemberLink&gt; SlaveLinks {get; set;}
      public virtual ICollection&lt;Message&gt;    ReceivedMessages {get; set;}
      public virtual ICollection&lt;Message&gt;    SentMessages {get; set;}
  }
  
  public partial class MemberLink
  {
      public int            RecordID     {get; set;}
      public virtual Member MasterMember {get; set;}
      public virtual Member SlaveMember  {get; set;}
      ...etc.
  }
  
  public partial class Message
  {
      public int            RecordID  {get; set;}
      public virtual Member Sender    {get; set;}
      public virtual Member Recipient {get; set;}
      ...etc.
  }
  </code></pre>
  
  <p>Now, the query I'm trying to perform is using the <strong>MemberLinkRepository</strong>, and looks like;</p>
  
  <pre><code>public IList&lt;MemberLink&gt; GetMasterLinks(int p_MemberID)
  {
      return Get()
             .Include ( memberLink =&gt; memberLink.MasterMember )
             .Include ( memberLink =&gt; memberLink.SlaveMember )
             .Include ( memberLink =&gt; memberLink.MasterMember.ReceivedMessages
                        .Where(
                        msg =&gt; msg.Sender.RecordID == memberLink.SlaveMember.RecordID) )
             .Where ( memberLink =&gt; memberLink.MasterMember.RecordID == p_MemberID)
             .ToList();
  </code></pre>
  
  <p>Except EF doesn't seem to like the nested Where. I could split this out into 2 separate repository calls (and indeed, it's looking like I might have to do that) but in the interest of reducing calls to the db I'm trying to do it in one foul swoop. Does anyone know how I can achieve this in one single query?</p>
  
  <p>I hope the code illustrates what I'm trying to do... If not, I'll try and explain a little better.</p>