Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda