Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that a Group can have Messages and a Message can have Groups, you are trying to maintain 5 things:</p> <ol> <li>The list of all Groups <code>List&lt;Group&gt; Groups = ...</code></li> <li>The list of all Messages <code>List&lt;Message&gt; messages = ...</code></li> <li>The messages for each Group <code>List&lt;Message&gt; GroupMessages...</code> in Group</li> <li>The groups for each message <code>List&lt;Group&gt; MessageGroup...</code> in Message</li> <li>The actual message to send to the group updated in several places</li> </ol> <p>From what I can see it's the last one that is not being maintained correctly:</p> <ul> <li><p>In <code>AddMessagetoGroup</code> you associate a 'new' <code>Message</code> to the <code>Group.GroupMessages</code>. This is a new instance of a class and won't be updated automatically when you update other Message instances. Just because two messages have the same <code>MessageId</code> doesn't mean they are the same instance.</p></li> <li><p>In <code>UpdateMessage</code> you change a particular message but only in the messages list. This doesn't point to the same message in the group list.</p></li> </ul> <p>All in all, you need a refactor to really get your code to what you want it to do. The way I see it is that you want to keep groups and messages separate, and reference once from the other rather than create copies. </p> <p>First, the master list:</p> <pre><code>List&lt;Group&gt; Groups = new List&lt;Group&gt;(); List&lt;Message&gt; Messages = new List&lt;Message&gt;(); </code></pre> <p>Secondly, creating <strong>or</strong> updating a message (you don't have the create part yet):</p> <pre><code>public Message CreateOrUpdateMessage(string messageID, string groupMessage) { var findmessage = Messages.Where(s =&gt; s.MessageID == messageID).FirstOrDefault(); if (findmessage != null) { findmessage.GroupMessage = groupMessage; } else { findmessage = new Message() { MessageID = messageID, GroupMessage = groupMessage}; Messages.Add(findmessage); } return findmessage; } </code></pre> <p>Note how this takes care of adding this message to the Messages list. This is the only function that should add or change messages.</p> <p>And finally adding messages to a group (note I don't worry about adding groups here):</p> <pre><code>public void AddMessagetoGroup(string group, string messageID, string message) { var findmessage = CreateOrUpdateMessage(messageID, message); var findgroup = Groups.Where(n =&gt; String.Equals(n.GroupName, group)).FirstOrDefault(); if (findgroup != null) { if (findgroup.GroupMessages.Where(m =&gt; m.MessageID == messageID).Count() == 0) { findgroup.GroupMessages.Add(findmessage); findmessage.MessageGroup.Add(findgroup); } } } </code></pre> <p>Note that this function will also create the message, and ensure there are no duplicates in either Messages in total or Messages for any particular group.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload