I am working on some c# I have inherited.
I have a problem in that something is being repeated twice in the output, I think I have identified in the controller where this is being added;
if(!isDirectUK)
rollingPriceSupp.Add(new KeyValuePair<string, float>("Personal Insurance",
(float)insuranceCost));
ViewData["vRollingPrice"] = rollingPriceSupp;
How can I put a check at that point to see if the string “Personal Insurance” has already been added, so as to avoid it bieng repeated twice ?
You must add an
check prior to the Add method.
UPDATE: Since seeing the OPs comment, the List is implemented using a
List<KeyValuePair>colletion. In this case the ContainsKey alone would not work.From the question alone I figured that they use some kind of Hashtable collection. In this case the containsKey would be sufficient.
If you cannot change the implementation from List to a Hashtable, then the solution would be to iterate through the items and check if it contains the key. E.g.
That should do the trick for you. Not the most efficient solution perhaps but should work.