在集合中一起添加Object的属性

时间:2011-01-20 14:46:10

标签: c# wpf db2

我有一个创建ContactList对象的应用程序,并将它们添加到Dictionary集合中。我的ContactList对象有一个名为AggLabels的属性,它是包含Name和Count属性的AggregatedLabel对象的集合。我想要做的是更改我的代码片段的“else”情况,以便在添加新的AggregatedLabel之前,它将检查AggregatedLabel集合中是否存在AggLabel.Name,如果这是真的,则不会添加AggLabel.Name再次。相反,它会将AggLabel.Count(int类型)的值添加到现有的AggregatedLabel对象中。因此,对于现有对象,如果第一个Count值为3而第二个值为2,那么新的Count值应为5.简单来说,我希望拥有唯一的AggLabel名称,并将名称相同的Counts加在一起。希望这是有道理的 - 会感激任何帮助。谢谢!

代码段

Dictionary<int, ContactList> myContactDictionary = new Dictionary<int, ContactList>();

        using (DB2DataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {

                int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);

                if (!myContactDictionary.ContainsKey(id))
                {

                    ContactList contactList = new ContactList();

                    contactList.ContactListID = id;
                    contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();

                    //contactList.AggLabels = new ObservableCollection<AggregatedLabel>() { new AggregatedLabel() { Name = dr["LABEL_NAME"].ToString(), Count = Convert.ToInt32(dr["LABEL_COUNT"])}};

                    contactList.AggLabels = new ObservableCollection<AggregatedLabel>()
                {
                    new AggregatedLabel()
                    {
                        Name = dr["LABEL_NAME"].ToString(),
                        Count = Convert.ToInt32(dr["LABEL_COUNT"])
                    }


                };
                    myContactDictionary.Add(id, contactList);
                }
                else
                {
                    ContactList contactList = myContactDictionary[id];
                    contactList.AggLabels.Add(
                        new AggregatedLabel()
                        {
                            Name = dr["LABEL_NAME"].ToString(),
                            Count = Convert.ToInt32(dr["LABEL_COUNT"])

                        }
                );


                }

            }
        }

3 个答案:

答案 0 :(得分:1)

我能想到两种可能的解决方案:

1)使用字典而不是聚合标签的集合,就像使用联系人字典一样。当您使用名称作为键并将计数作为值时,可以使用ContainsKey-Method检查标签是否已存在。

contactList.AggLabels = new Dictionary<string, int>();

...     其他     {         ContactList contactList = myContactDictionary [id];

    if (contactList.AggLabels.ContainsKey(dr["LABEL_NAME"].ToString()))
    {
        contactList.AggLabels[dr["LABEL_NAME"].ToString()] += Convert.ToInt32(dr["LABEL_COUNT"]);
    }
    else
    {
        contactList.AggLabels.Add(dr["LABEL_NAME"].ToString(), Convert.ToInt32(dr["LABEL_COUNT"]));
    }
}

2)我需要使用AggreagteLabel对象,你可以使用循环来搜索所有标签。

else
{
    bool flagAggLabelFound = false;
    ContactList contactList = myContactDictionary[id];

    foreach(AggregateLabel aggLabel in contactList.AggLabels)
    {
        if(aggLabel.Name == dr["LABEL_NAME"].ToString())
        {
            aggLabel.Count += Convert.ToInt32(dr["LABEL_COUNT"]);
            flagAggLabelFound = true;
            break;
        }
    }

    if (!flagAggLabelFound)
    {
        contactList.AggLabels.Add(
                        new AggregatedLabel()
                        {
                            Name = dr["LABEL_NAME"].ToString(),
                            Count = Convert.ToInt32(dr["LABEL_COUNT"])

                        }
        );
    }
}

我希望这会有所帮助。

答案 1 :(得分:0)

我会试试这个:

ContactList contactList = myContactDictionary[id];
AggregateLabel existing = contactList.AggLabels.FirstOrDefault(
                              l => l.Name == dr["LABEL_NAME"].ToString()
                          );
if (existing == null) { contactList.AggLabels.Add(
                            new AggregatedLabel() {
                                Name = dr["LABEL_NAME"].ToString(),
                                Count = Convert.ToInt32(dr["LABEL_COUNT"])
                            }
                        );
                      }
else { existing.Count += Convert.ToInt32(dr["LABEL_COUNT"]); }

答案 2 :(得分:0)

@extract这些聚合标签并将它们放在一个单独的Observable集合中:

1)如果你是一个用于在联系人列表中存储标签的词典,这应该有效:

ObservableCollection<AggregateLabel> copyOfAggregateLabels = new ObservableCollection<AggregateLabel>();

    foreach (KeyValuePair<string, int> aggLabel in aggregateLabels)
    {
        copyOfAggregateLabels.Add(
            new AggregatedLabel() {
                Name = aggLabel.Key,
                Count = aggLabel.Value
            }

        );
    }

2)如果使用AggregateLabels的ObservableCollection,则会在循环中获得AggregateLable而不是KeyValuePair。其余的工作方式相同。

首先我想到了这样的事情:

ObservableCollection<AggregateLabel> copyOfAggregateLabels = new ObservableCollection<AggregateLabel>(aggregateLables);

但是这样你得到一个新的ObservableCollection,但是新集合中存储的标签仍然引用与你复制的集合中的标签相同的对象。

相关问题