dimanche 27 septembre 2020

for loop desired instead of nested if statements

I am building a website that does has a chat component to it. The code below receives from a stored procedure a list of messages with a lots of different paramaters. 1 of those is if a message is in reply to another, and if that is the case, duplicate the message that is being replied to over the message answer. If the message that was being replied to was also an answer to a previous message do the same ect. Now my issue is that I have not been able to figure out how to automate this part of the code without nesting if into one another until a point where I hope users won't reply in the same chain anymore.

  • To rephrase it, I go in a list in inverse order and check if the ReplyingTo is not null.
  • I then copy the row that has the same ID and ReplyingTo 1 row higher than the current row.
  • I then confirm that this new row has a ReplyingTo
  • If it does I copy that object 2 row higher than the current one.
  • and I would continue this way until I reached a certain point that the users would not reach.

If anyone got an idea on how to proceed I would be highly gracious. I have put an example of the type of data that would be given to this function below.

             for (int i = publicChatCountList.Count-1 ; i > -1; i--)
                {
                    if (publicChatCountList[i].ReplyingTo.HasValue)
                    {
                        Chat_Dto chatItem = new Chat_Dto();
                        long? ReplyingToId = publicChatCountList[i].ReplyingTo;
                        chatItem = publicChatCountList.Find(x => x.Id == ReplyingToId);
                        publicChatCountList.Insert(i+1, new Chat_Dto() {Text = chatItem.Text, IsPublic = chatItem.IsPublic, IsApproved = chatItem.IsApproved, ReplyingTo = chatItem.ReplyingTo });
                        publicChatCountList[i+1].Duplicate = true;

                        if (chatItem.ReplyingTo.HasValue)
                        {
                            Chat_Dto chatItem2 = new Chat_Dto();
                            long? ReplyingToId2 = chatItem.ReplyingTo;
                            chatItem2 = publicChatCountList.Find(x => x.Id == ReplyingToId2);
                            publicChatCountList.Insert(i + 2, new Chat_Dto() { Text = chatItem2.Text, IsPublic = chatItem2.IsPublic, IsApproved = chatItem2.IsApproved, ReplyingTo = chatItem2.ReplyingTo });
                            publicChatCountList[i + 2].Duplicate = true;
                        }
                    }
                }

enter image description here

Aucun commentaire:

Enregistrer un commentaire