Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
[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);
    }

Now I want to set the remaining 9 parameters values using other foreach loop which I am trying with below code below.
C#
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);
    }
}

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:

C#
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);
    }
}
Posted
Updated 25-Apr-16 20:53pm
v2

Add this in your second loop

C#
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);
                    }
                }

            }
 
Share this answer
 
v2
Comments
Telstra 26-Apr-16 3:14am    
Hi Karthik,
Thanks for your reply. Will this loop modify existing dictionary and add the values to the remaining properties.
Thanks,
Nilesh
Karthik_Mahalingam 26-Apr-16 3:15am    
Yes nilesh.
You can debug and check it
Telstra 26-Apr-16 3:18am    
where should I check the last updated dictionary not sure where to please debugger to check the dictionary.
Thanks,
Nilesh
Karthik_Mahalingam 26-Apr-16 3:22am    
On your second list to update the remaining properties
Telstra 26-Apr-16 3:47am    
yupee..got it..its getting updated..Thank u very much for your help..:):)
Nilesh
You can try this also

C#
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;
                   }
               }
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900