title: Filtering with EF Core through multiple nested object relationships
body: <p>I have a new requirement to filter reports return from a query based on what amounts to a blacklist.</p>
  <p>So originally I had this query which brings back all the pertinent information required and gets a list of reports associated to that particular user.</p>
  <pre><code>var reports = from r in Context.Reports
                            join ax in Context.AuthorizationXref on r.Id equals ax.ReportId
                            join g in Context.Groups on ax.GroupId equals g.Id
                            join ugx in Context.UsersGroupsXref on g.Id equals ugx.GroupId
                            where ugx.UserId == id
                            select r;
  </code></pre>
  <p>AuthorizationXref has ReportID and GroupId.</p>
  <pre><code>public partial class AuthorizationXref
      {
          public int Id { get; set; }
          public int ReportId { get; set; }
          public int GroupId { get; set; }
  
          public virtual Groups Group { get; set; }
          public virtual Reports Report { get; set; }
      }
  
  public partial class Groups
      {
          public Groups()
          {
              AuthorizationXref = new HashSet&lt;AuthorizationXref&gt;();
              UsersGroupsXref = new HashSet&lt;UsersGroupsXref&gt;();
          }
  
          public int Id { get; set; }
          public string Name { get; set; }
          public string Description { get; set; }
  
          public virtual ICollection&lt;AuthorizationXref&gt; AuthorizationXref { get; set; }
          public virtual ICollection&lt;UsersGroupsXref&gt; UsersGroupsXref { get; set; }
      }
  </code></pre>
  <p>User many to many Groups many to many Reports (the many to many is done through the XREFs).</p>
  <p>While in here I have benched it at 700ms which seems really slow and I realize I am doing at least 4 hits to the DB so I tried to do this which amounted to the same data more or less but ~7 times faster:</p>
  <pre><code>    var repo = context.Reports
          .Include(x =&gt; x.AuthorizationXref)
          .ThenInclude(x =&gt; x.Group)
          .ThenInclude(x =&gt; x.UsersGroupsXref)
          .ToList();
  </code></pre>
  <p>This benched at about 100ms but doesn't do any filtering on userId. These benchmarks are in Dev and will only worsen as we get into the higher environments where more and more reports are added to users. I know that using Selects is more efficient but I can't find examples of complex nested many-to-many selects on entities.</p>
  <p>I can get about this far but don't know where to put the additional steps to drill deeper into the object.</p>
  <pre><code>var asdf = repo.Where(x=&gt;x.AuthorizationXref.Any(y=&gt;y.ReportId==x.Id));
  </code></pre>
  <p>End Goal is I need a list of Reports by userId removing those reports that appear by ID on another table. So there is a table called UserReportFilter and it has ReportId and UserId, any reports in that table should not appear in my end result.</p>
  <p>As a side note if anyone can point me in the direction of a tutorial (preferably one that doesn't assume the reader knows absolutely everything) on how to use the Expressions I would appreciate it. I ran across <a href="https://benjii.me/2018/01/expression-projection-magic-entity-framework-core/" rel="nofollow noreferrer">this article</a> and it would seem like a powerful thing to learn for things like this, however I will need a bit more meat in the explanation. I understand the concept and I have used basic Func returns for queries but nothing that extensive.</p>