title: How can I fully load a recursive model on multiple levels with entity framework?
body: <p><strong>EDIT: This is the answer <a href="https://stackoverflow.com/questions/41894751/is-recursive-query-possible-in-linq/41909322#41909322">Is recursive query possible in LINQ</a>
  It was already answered there must have missed that one.</strong></p>
  
  <p>I'm on .NET Core 2.2.</p>
  
  <p>I've looked through multiple questions asked on this site and none of the answers worked for me.</p>
  
  <p>My Model looks like this (simplified):</p>
  
  <pre><code>public class Product
  {
      /// &lt;summary&gt;
      ///     Gets or sets the Id.
      /// &lt;/summary&gt;
      public string Id { get; set; }
  
      /// &lt;summary&gt;
      ///     Gets or sets the code.
      /// &lt;/summary&gt;
      public string Code { get; set; }
  
      /// &lt;summary&gt;
      ///     Gets or sets the properties.
      /// &lt;/summary&gt;
      public List&lt;Property&gt; Properties { get; set; }
  
      /// &lt;summary&gt;
      ///     Gets or sets the products
      /// &lt;/summary&gt;
      public List&lt;Product&gt; Products { get; set; }
  }
  
  public class Property
  {
      /// &lt;summary&gt;
      ///     Gets or sets the Id.
      /// &lt;/summary&gt;
      public string Id { get; set; }
  
      /// &lt;summary&gt;
      ///     Gets or sets the code
      /// &lt;/summary&gt;
      public string Code { get; set; }
  }
  </code></pre>
  
  <p>I am trying to load a product with all it's sub products also including the properties of all the products.</p>
  
  <p>First, I've been able to load all products recursively but I honestly don't know why it works.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>var products1 = this._context.Products.Where(x =&gt; x.Code == "TEST").Include(x =&gt; x.Products);
  </code></pre>
  
  <p>And it would only load the products of the first level products.</p>
  
  <p>Then by pure luck/mistake I've left this line before the aforementioned one:</p>
  
  <pre><code>this._context.Products.ToList()
  </code></pre>
  
  <p>Then, the first line actually loaded all products recursively... nice I guess?</p>
  
  <p>Anyway, now what I'm trying to do is load all products recursively while also loading all of their properties.</p>
  
  <p>I've tried a couple of things including the shady-looking:</p>
  
  <pre><code>this._context.Products.Where(x =&gt; x.Code == "TEST").Include(x =&gt; x.Properties)
              .Include(x =&gt; x.Products).ThenInclude(x =&gt; x.Properties);
  </code></pre>
  
  <p>But whatever I do I will only load the properties of the products of the first level of recursion. 
  I still fully load all products recursively but only the first 2 levels have their properties loaded.</p>
  
  <p>So ultimately my question is how do I do that? As a bonus question that might be answered anyway if the real question is answered: why do I need to put this line: </p>
  
  <pre><code>this._context.Products.ToList()
  </code></pre>
  
  <p>before this line:</p>
  
  <pre><code>var products1 = this._context.Products.Where(x =&gt; x.Code == "TEST").Include(x =&gt; x.Products);
  </code></pre>
  
  <p>To load the products recursively?</p>
  
  <p><strong>EDIT: Note that I know I can recursively load all Product/Property with multiple queries (one for each Product) in a loop but I definitely want to avoid that if possible.</strong></p>