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 identifi开发者_开发问答ed 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
if(rollingPriceSupp.ContainsKey("Personal Insurance"))
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.
bool bItemExists = false;
foreach(KeyValuePair<string, float> pItem in rollingPriceSupp)
{
if(pItem.Key == "Personal Insurance")
{
bItemExists = true;
break;
}
}
if(!bItemExists)
{
rollingPriceSupp.Add(new KeyValuePair<string, float>("Personal Insurance",
(float)insuranceCost));
}
That should do the trick for you. Not the most efficient solution perhaps but should work.
The IEnumerable extension methods offer several possibilities, including Count:
var count = rollingPriceSupp.Count(x => x.Key.Equals("Personal Insurance"));
var containsMultiple = count > 1;
The Where or FirstOrDefault methods might be good options also.
If, as Nikos Steiakakis says, you are using a List<KeyValuePair<string, float>>
, then my question to you is: why? Particularly if you don't want duplicate keys, you should definitely be using a Dictionary<string, float>
, a nice little side-benefit of which would be that you don't have to write:
rollingPriceSupp.Add(new KeyValuePair<string, float>("Personal Insurance",
(float)insuranceCost));
Rather, you can simply write:
rollingPriceSupp.Add("Personal Insurance", (float)insuranceCost));
If random access by index is necessary, then I suppose you may indeed need to use a List
. In that case you can use the FindIndex
method (no need for extensions):
// this function will give you a Predicate to check for any key
public Predicate<KeyValuePair<string, float>> GetMatcher(string key) {
return (KeyValuePair<string, float> item) => { return item.Key == key; };
}
int index = rollingPriceSupp.FindIndex(GetMatcher("Personal Insurance"));
bool keyExists = (index != -1);
if (!keyExists && !isDirectUK) {
rollingPriceSupp.Add(new KeyValuePair<string, float>("Personal Insurance",
(float)insuranceCost));
}
Be aware, however, that this approach (a brute-force search for a duplicate key) is going to require iterating and will therefore have O(n) performance, which may or may not be acceptable depending on the size of your List
. If performance is a priority and you can afford to allocate some extra memory, you might want to consider maintaining a HashSet<string>
side-by-side with your list, so that when you add you do this:
HashSet<string> rollingPriceSuppKeys = new HashSet<string>();
if (!rollingPriceSuppKeys.Contains("Personal Insurance") && !isDirectUK) {
rollingPriceSupp.Add(new KeyValuePair<string, float>("Personal Insurance",
(float)insuranceCost));
rollingPriceSuppKeys.Add("Personal Insurance");
}
Use the ContainsKey(key) instance method on your dictionary object.
精彩评论