linq issue with creating relationships

余生颓废 提交于 2020-01-05 12:35:01

问题


Seems I have a slight problem with my linq, I have a datacontract for Groups and I have a seperate datacontract for messages. Messages can be part of a Group. However when I update a message record its not reflected when I list the group information, the message is still the same for that group. But yet the update is reflected when I directly list messages?

This is how I add a message to a group:

    //lists for reference:
    List<Group> Groups = new List<Group>();
    List<Message> messages = new List<Message>();

    //not sure the below method is correct for adding a message to a group
    public void AddMessagetoGroup(string group, string messageID, string message)
    {
        var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
        var result1 = messages.Where(n => String.Equals(n.MessageID, messageID)).FirstOrDefault();
        if (result != null)
        {
            result.GroupMessages.Add(new Message() { MessageID = messageID, GroupMessage = message });
        }
        if (result1 != null)
        {
            result1.MessageGroup.Add(new Group() { GroupName = group });
        }

    }

Seriously dont understand whats going on, if I add the message to the group any changes I make to the message should be reflected. The only thing I can think of is its adding a new instance of the already existing message, which means my update method is only copying the message, but where is this new copyed record even held. If its to difficult to fix how could I update the message that has been copyedTo/added to the group instead (cheap workaround)?


回答1:


Assuming that a Group can have Messages and a Message can have Groups, you are trying to maintain 5 things:

  1. The list of all Groups List<Group> Groups = ...
  2. The list of all Messages List<Message> messages = ...
  3. The messages for each Group List<Message> GroupMessages... in Group
  4. The groups for each message List<Group> MessageGroup... in Message
  5. The actual message to send to the group updated in several places

From what I can see it's the last one that is not being maintained correctly:

  • In AddMessagetoGroup you associate a 'new' Message to the Group.GroupMessages. 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 MessageId doesn't mean they are the same instance.

  • In UpdateMessage you change a particular message but only in the messages list. This doesn't point to the same message in the group list.

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.

First, the master list:

List<Group> Groups = new List<Group>();
List<Message> Messages = new List<Message>();

Secondly, creating or updating a message (you don't have the create part yet):

public Message CreateOrUpdateMessage(string messageID, string groupMessage)
{
    var findmessage = Messages.Where(s => s.MessageID == messageID).FirstOrDefault();

    if (findmessage != null)
    {
        findmessage.GroupMessage = groupMessage;
    }
    else
    {
        findmessage = new Message() { MessageID = messageID, GroupMessage = groupMessage};
        Messages.Add(findmessage);
    }

    return findmessage;
}

Note how this takes care of adding this message to the Messages list. This is the only function that should add or change messages.

And finally adding messages to a group (note I don't worry about adding groups here):

public void AddMessagetoGroup(string group, string messageID, string message)
{
    var findmessage = CreateOrUpdateMessage(messageID, message); 
    var findgroup = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();

    if (findgroup != null)
    {
        if (findgroup.GroupMessages.Where(m => m.MessageID == messageID).Count() == 0)
        {
            findgroup.GroupMessages.Add(findmessage);
            findmessage.MessageGroup.Add(findgroup);
        }
    }
}

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.



来源:https://stackoverflow.com/questions/10345489/linq-issue-with-creating-relationships

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!