如何在C#中为字典中的现有键添加值 [英] How to add values to the existing keys in the dictionary in C#

查看:333
本文介绍了如何在C#中为字典中的现有键添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它在字典中返回7个记录集合< int,nonexuscounterparty>格式。 NoNexusCounterparty是一个定义了17个属性的类。每条记录都将int作为键,将类对象作为值。 classObject值有17个参数。其中我在下面的代码中为每个循环设置了8个参数值,这是正常工作的。

I Have the following code which returns me 7 records collection in the dictionary<int,nonexuscounterparty> format. NoNexusCounterparty is a class in which 17 properties defined. Each record have int as a key and class object as a value. classObject value have 17 parameters. Out of which I am setting the 8 parameters values in first for each loop in below code which is working fine.

[AcceptVerbs(HttpVerbs.Get)]
public ContentResult GetAllNexusFavs()
{
    string refreshTimestamp = _utilityProvider.GetCclDatabaseTimeStamp();
    //List<NoNexusCounterparty> cps = new List<NoNexusCounterparty>();
    
    CounterpartyQuery query = this.CreateCounterpartyQuery();

    _log.InfoFormat("GetAllNexusFavs start " + query.ToString());

    CounterpartyResponse cpResponse = new CounterpartyResponse();
    CounterpartyResponse cpASICResponse = new CounterpartyResponse();
    CounterpartyResponse cpMASResponse = new CounterpartyResponse();
    CounterpartyResponse cpHKGResponse = new CounterpartyResponse();

    Dictionary<int, NoNexusCounterparty> noNexusCpsdict = new Dictionary<int, NoNexusCounterparty>();

    if (query.beId != "-1")
    {
        cpResponse = _counterpartyProvider.GetFavorites(query);//no cross border
    }

    foreach (var counterparty in cpResponse.counterpartyList)
    {
        NoNexusCounterparty cps = new NoNexusCounterparty();
        cps.NoNexuseligibleInd = counterparty.EligibleInd;
        cps.NoNexuseligibleInd = counterparty.EligibleInd;
        cps.NonSEFNoNexusEligibleInd = counterparty.NonSEFEligibleInd;
        cps.SEFNoNexusEligibleInd = counterparty.SEFEligibleInd;
        cps.CounterpartyId = counterparty.CounterpartyId;
        cps.CounterpartyName = counterparty.CounterpartyName;
        cps.BuyingCentreId = counterparty.CounterpartyId;
        cps.BuyingCentreName = counterparty.BuyingCentreName;
        cps.cftcDis = counterparty.cftcDis;
        noNexusCpsdict.Add(cps.CounterpartyId, cps);
    }



现在我想使用其他foreach循环设置剩余的9个参数值,我正在尝试下面的代码。


Now I want to set the remaining 9 parameters values using other foreach loop which I am trying with below code below.

List<KeyValuePair<int, NoNexusCounterparty>> listcps = noNexusCpsdict.ToList();
query.IsNexusASICInScope = 1;

cpASICResponse = _counterpartyProvider.GetFavorites(query);

foreach (KeyValuePair<int,NoNexusCounterparty> pair in listcps )
{
    foreach (var counterparty in cpResponse.counterpartyList)
    {
        cps.SEFASICEligibleInd = counterparty.SecEligibleInd;
        cps.NonSEFASICEligibleInd = counterparty.NonSEFEligibleInd;
        cps.ASICeligibleInd = counterparty.EligibleInd;
        noNexusCpsdict.Add(cps.CounterpartyId, cps);
    }
}



但是它会抛出错误已经添加了具有相同密钥的项目。

如何在每个循环的第一个字段对象中使用相同的字典对象及其键,以便针对每个键设置其他9个参数。



我尝试过:




But it is throwing an error as An item with the same key has already been added.
How I can use the same dictionary object retrn in the first for each loop and its keys to set the other 9 parameters against the each key.

What I have tried:

List<keyvaluepair><int,>> listcps = noNexusCpsdict.ToList();
query.IsNexusASICInScope = 1;

cpASICResponse = _counterpartyProvider.GetFavorites(query);

foreach (KeyValuePair<int,nonexuscounterparty> pair in listcps )
{
    foreach (var counterparty in cpResponse.counterpartyList)
    {
        NoNexusCounterparty cps = new NoNexusCounterparty();
        cps.SEFASICEligibleInd = counterparty.SecEligibleInd;
        cps.NonSEFASICEligibleInd = counterparty.NonSEFEligibleInd;
        cps.ASICeligibleInd = counterparty.EligibleInd;
        noNexusCpsdict.Add(cps.CounterpartyId, cps);
    }
}

推荐答案

在第二个循环中添加此内容



Add this in your second loop

foreach (KeyValuePair<int, nonexuscounterparty> pair in listcps)
            {
                foreach (var counterparty in cpResponse.counterpartyList)
                {
                    if (noNexusCpsdict.ContainsKey(counterparty.CounterpartyId))
                    {
                        noNexusCpsdict[counterparty.CounterpartyId].SEFASICEligibleInd = counterparty.SecEligibleInd;
                        noNexusCpsdict[counterparty.CounterpartyId].NonSEFASICEligibleInd = counterparty.NonSEFEligibleInd;
                        noNexusCpsdict[counterparty.CounterpartyId].ASICeligibleInd = counterparty.EligibleInd;
                        noNexusCpsdict.Add(cps.CounterpartyId, cps);
                    }
                }

            }


你也可以尝试这个



You can try this also

foreach (KeyValuePair<int, NoNexusCounterparty> x in noNexusCpsdict)
           {
               foreach (var counterparty in cpResponse.counterpartyList)
               {
                   if (x.Key.Equals(counterparty.CounterpartyId))
                   {
                       x.Value.SEFASICEligibleInd = counterparty.SecEligibleInd;
                       x.Value.NonSEFASICEligibleInd = counterparty.NonSEFEligibleInd;
                       x.Value.ASICeligibleInd = counterparty.EligibleInd;
                   }
               }
           }


这篇关于如何在C#中为字典中的现有键添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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