2

I'm trying to make a discussion board similar to Reddit where a Post can have multiple Comments, each Comment can have multiple Comments, and each of those Comments can have multiple Comments, and so on.

How would I write a query to return a specific Post which includes all of its Comments, along with all the Comments' comments, and all of those Comments' comments, etc.? I thought about using .ThenInclude() but it would be impossible since I don't know how many nested comments there are ahead of time.

This is what I have currently but it only retrieves direct replies to the Post, and none of the nested comments:

selected = await context.Posts
                    .Include(p => p.Author)
                    .Include(p => p.SavedBy)
                    .Include(p => p.HiddenBy)
                    .Include(p => p.UpvotedBy)
                    .Include(p => p.DownvotedBy)
                    .Include(p => p.Replies.Where(c => !c.HiddenBy.Contains(user)))
                        .ThenInclude(c => c.Author)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.SavedBy)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.HiddenBy)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.UpvotedBy)
                    .Include(p => p.Replies)
                        .ThenInclude(c => c.DownvotedBy)
                    .SingleAsync(p => p.Id == id);

Model:

public abstract class Entry
    {
        public Entry()
        {
            Replies = new List<Comment>();
            SavedBy = new List<ApplicationUser>();
            HiddenBy = new List<ApplicationUser>();
            UpvotedBy = new List<ApplicationUser>();
            DownvotedBy = new List<ApplicationUser>();
        }

        public int Id { get; set; }
        public string Content { get; set; }
        public DateTime DateCreated { get; set; }
        public int Upvotes { get; set; }
        public int Downvotes { get; set; }
        public int VoteScore { get; set; }

        public ApplicationUser Author { get; set; }
        public ICollection<Comment> Replies { get; set; }
        public ICollection<ApplicationUser> SavedBy { get; set; }
        public ICollection<ApplicationUser> HiddenBy { get; set; }
        public ICollection<ApplicationUser> UpvotedBy { get; set; }
        public ICollection<ApplicationUser> DownvotedBy { get; set; }
    }

    public class Post : Entry
    {
        public string Title { get; set; }
    }

    public class Comment : Entry
    {
        public Entry RepliedTo { get; set; }
        public Post Post { get; set; }
    }

DbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        public DbSet<Entry> Entries { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<Comment> Comments { get; set; }
    }

1 Answer 1

1

Think about how the query would look in SQL instead of C#/LINQ for a moment. Since the relationships would be recursive, you would need to explicitly define the joins for as many layers are require to prefetch. That leaves you with missing data for any layers beyond. Alternatively, you could use a cursor bound up by a stored procedure. No. How about, instead, a comment can have a pointer to its parent comment, but also have a foreign key to the MOST parent entity (the original post). Then, when you get all the comments for a post, you can retrieve all the comments as a flat array, then build the tree in c# (outside of SQL). This should be a lot more performant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.