title: EF Core - Issue with including list of child property which have their own list of child properties
body: <p>I am having trouble figuring out how to include a list of child property which can have its own list of child property.</p>
  
  <p>My entity is setup as below:</p>
  
  <pre><code>public class Comment
      {
          [Key]
          public Guid Id { get; set; }
  
          //other properties
  
          public Guid? ParentCommentId { get; set; }
  
          public Comment ParentComment { get; set; }
  
          public ICollection&lt;Comment&gt; ChildComments { get; set; }
  
          [Required]
          public Guid PostId { get; set; }
  
          [Required]
          public Post CommentForPost { get; set; }
  
          [Required]
          public Guid UserId { get; set; }
  
          [Required]
          public User CreatedBy { get; set; }
      }
  </code></pre>
  
  <p>I have below relationships:</p>
  
  <ul>
  <li>One-to-many relationship between <code>Post</code> -> <code>Comment</code></li>
  <li>One-to-many relationship between <code>User</code> -> <code>Comment</code></li>
  <li>One-to-many relationship between <code>Comment</code> -> <code>Comment</code></li>
  </ul>
  
  <p>What I want to do is when I query the comments for a post, include the parent level comment, and all of the parent's child comments (i.e. the <code>ICollection of ChildComments</code> property).</p>
  
  <p>Currently I have below LINQ statement setup:</p>
  
  <pre><code>return _context.Comments.Include(comment =&gt; comment.CreatedBy)
                          .Include(comment =&gt; comment.ChildComments)
                          .ThenInclude(x =&gt; x.CreatedBy)
                          .Where(comment =&gt; comment.PostId == postId)
                          .OrderBy(comment =&gt; comment.Created)
                          .AsNoTracking();
  </code></pre>
  
  <p>The problem is that I am also getting duplicates. For example, if I test above with below data:</p>
  
  <pre><code>Comment1 {
    Id = 1
    ChildComments = Comment2
  }
  Comment2 {
    Id = 2
    ParentCommentId = 1
    ChildComments = Comment3
  }
  Comment3 {
    Id = 3
    ParentCommentId = 2
    ChildComments = null
  }
  Comment4 {
    Id = 4
    ChildComments = null
  }
  </code></pre>
  
  <p>If I use above query, I end up getting below. What I want to get back is just a list of parent level comments, and all their child comments (and if the child comments have their own child comments, include them as well).</p>
  
  <p><strong>Incorrect:</strong></p>
  
  <pre><code>[
      {
          "id": "1",       
          "parentCommentId": null,
          "childComments": [
              {
                  "id": "2",                
                  "parentCommentId": "1",
                  "childComments": [
                      {
                          "id": "3",                        
                          "parentCommentId": "2",
                          "childComments": []
                      }
                  ]
              }
          ]
      },
      {
          "id": "2",       
          "parentCommentId": "1",
          "childComments": [
              {
                  "id": "3",               
                  "parentCommentId": "2",
                  "childComments": []
              }
          ]
      },
      {
          "id": "3",       
          "parentCommentId": "2",
          "childComments": []
      },
      {
          "id": "4",       
          "parentCommentId": null,
          "childComments": []
      }
  ]
  </code></pre>
  
  <hr>
  
  <h2>What I want to get back:</h2>
  
  <pre><code>[
      {
          "id": "1",       
          "parentCommentId": null,
          "childComments": [
              {
                  "id": "2",                
                  "parentCommentId": "1",
                  "childComments": [
                      {
                          "id": "3",                        
                          "parentCommentId": "2",
                          "childComments": []
                      }
                  ]
              }
          ]
      },
      {
          "id": "4",       
          "parentCommentId": null,
          "childComments": []
      }
  ]
  </code></pre>
  
  <hr>
  
  <p><strong>Update:</strong> I have tried adding in a where clause for <code>ParentCommentId == null</code>so the modified query as:</p>
  
  <pre><code>return _context.Comments.Include(comment =&gt; comment.CreatedBy)
                                      .Include(comment =&gt; comment.ChildComments)
                                      .ThenInclude(x =&gt; x.CreatedBy)
                                      .Where(comment =&gt; comment.PostId == postId)
                                      .Where(comment =&gt; comment.ParentCommentId == null)
                                      .OrderBy(comment =&gt; comment.Created)
                                      .AsNoTracking();
  </code></pre>
  
  <p>But the issue is that with above linq statement, I am not getting children of children. I.e. with my example above, I am getting below instead of the correct answer.</p>
  
  <pre><code>[
      {
          "id": "1",       
          "parentCommentId": null,
          "childComments": [
              {
                  "id": "2",                
                  "parentCommentId": "1",
                  "childComments": []
              }
          ]
      },
      {
          "id": "4",       
          "parentCommentId": null,
          "childComments": []
      }
  ]
  </code></pre>