与建立关系的linq问题 [英] linq issue with creating relationships

查看:110
本文介绍了与建立关系的linq问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我的linq有点问题,我有一个Groups的数据合同,而我有一个单独的消息数据合同.邮件可以是组的一部分.但是,当我更新消息记录时,在列出组信息时未反映该消息记录,该消息对于该组仍然是相同的.但是,当我直接列出邮件时,反映了更新吗?

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)?

推荐答案

假设一个组可以有消息,而一个消息可以有组,则您要维护5件事:

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

  1. 所有组的列表List<Group> Groups = ...
  2. 所有消息的列表List<Message> messages = ...
  3. 组中每个组List<Message> GroupMessages...的消息
  4. 邮件中每个邮件List<Group> MessageGroup...的组
  5. 要发送给网上论坛的实际消息已在多个地方更新
  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:

  • AddMessagetoGroup中,将新" MessageGroup.GroupMessages相关联.这是类的新实例,更新其他Message实例时不会自动更新.仅仅因为两个消息具有相同的MessageId并不意味着它们是相同的实例.

  • 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.

UpdateMessage中,您可以更改特定的消息,但只能在消息列表中.这并不指向组列表中的同一条消息.

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.

首先,主列表:

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.

这篇关于与建立关系的linq问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆