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();
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);
}
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.
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:
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);
}
}