Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
ASP.NET MVC recursion

I am working with .NET MVC that have a model implement recursion as below:

    public class Comment
{
    [Key]
    [Required]
    public int CommentId { get; set; }

    [Required]
    public string Content { get; set; }
    public bool Anonymous { get; set; }
    [Required]
    public DateTime Created_date { get; set; }

    [Required]
    public DateTime Last_modified { get; set; }
    public virtual Comment Reply { get; set; }
    public virtual ICollection<Comment> Replys { get; set;}
    public virtual Idea Idea { get; set; }
}

In explanation, each Idea contains different Comments inside it, and each comment also has several smaller Comments that reply to the previous one. However, I have no idea how to do recursion to get replies for a comment and smaller reply for each reply until the last one in controller and also how to display them in view. Feel free to ask me any question since my explanation is not clear sometimes.

  public ActionResult Index(int? i)
    {
        List<Idea> Ideas = db.Ideas.ToList();
        foreach(Idea idea in Ideas)
        {
            idea.Comments = db.Comments.Include(x => x.Reply).ToList();
            idea.Comments=idea.Comments.Where(x => x.Idea == idea).ToList();
            foreach (Comment comment in idea.Comments)
            {
                comment.Replys = GetComments(comment.CommentId); //This function use to get list of reply for comment
                foreach (Comment reply in comment.Replys)
                {
                    reply.Replys=GetComments(reply.CommentId);
                    foreach (Comment reply2 in reply.Replys)
                    {
                        reply2.Replys=GetComments(reply2.CommentId);
                        foreach(Comment reply3 in reply2.Replys)
                        {
                            reply3.Replys = GetComments(reply3.CommentId);
                            //When would this stop ?
                        }
                    }
                }
            }
        }
        return View(Ideas.ToPagedList(i ?? 1, 5));
      
    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use this loop;

foreach(Idea idea in Ideas)
{
    idea.Comments = db.Comments.Include(x => x.Reply).ToList();
    idea.Comments= idea.Comments.Where(x => x.Idea == idea).ToList();
    
    foreach (Comment comment in idea.Comments)
    {
        RecursionGetComments(comment);
    }
}

Add this function;

public void RecursionGetComments(Comment comment){
    comment.Replys = GetComments(comment.CommentId);

    foreach(var reply in comment.Replys){
        RecursionGetComments(reply);
    }
}

If the RecursionGetComments above didn't fill properly, you might need to use the ref keyword which passes the current instance of the class (pass by reference);

public void RecursionGetComments(ref Comment comment){
    comment.Replys = GetComments(comment.CommentId);

    foreach(var reply in comment.Replys){
        RecursionGetComments(reply);
    }
}

I also noticed that in your model, you are using the keyword virtual.

Virtual signals entity framework that the navigation property will be loaded automatically / lazy loading, since you are loading your comments manually, you could remove that.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!