title: C# Entity Framework: Linq Filter on GrandChildren and Conduct a Select on the Parent
body: <p>Our company is currently using Entity Framework Net Core 2.2 with Sql Server</p>
  <p>Trying to find all Distinct customers who purchased a certain Product Input Parameter.
  When trying to do final select, it shows b lambda as Product. We need the Distinct Customers showing up last.</p>
  <p>How the EF Linq query be written to get this for distinct Customers?</p>
  <pre><code>var taxAgencyDistinctList = db.Customer
          .SelectMany(b =&gt; b.Transactions)
          .SelectMany(b =&gt; b.Purchases)
          .Select(b =&gt; b.Product)
          .Where(b =&gt; b.BKProduct == ProductInput) 
          .Select(b =&gt; b.).Distinct();
  </code></pre>
  <p>Equivalent SQL is easy:</p>
  <pre><code>select distinct c.customerName
  from dbo.customer customer
  inner join dbo.Transactions transaction
      on transaction.customerid = customer.customerid
  inner join dbo.Purchases purchases
      on purchases.PurchaseId = transaction.PurchaseId
  inner join dbo.Product product 
      on transaction.ProductId = product.ProductId
  where tra.BKProduct = @ProductInput
  </code></pre>
  <p>Company prefers method, where we Don't use Linq to Sql, if possible</p>
  <p><strong>Resources:</strong></p>
  <p><a href="https://stackoverflow.com/questions/43618096/filtering-on-include-in-ef-core?noredirect=1&amp;lq=1">Filtering on Include in EF Core</a></p>
  <p><a href="https://stackoverflow.com/questions/61861869/filtering-on-theninclude-three-nested-levels-down">Filtering On ThenInclude Three Nested Levels down</a></p>
  <p><a href="https://stackoverflow.com/questions/57681950/net-core-entity-framework-theninclude-with-projection-select">Net Core: Entity Framework ThenInclude with Projection Select</a></p>