Following is my function:-
public void deleteAllCarsWithType(Dictionary < KeyValuePair<string, string> , car > dictCar, string typeOfCar)
{
var keyValuePairs = dictCar.Where(keyValuePair => keyValuePair.Key.Equals(typeOfCar));
//var count = keyValuePairs.Count();
//this also gives the size of keyValuePairs as 0.
foreach (var keyValuePair in keyValuePairs)
{
dictCar.Remove(keyValuePair.Key);
}
}
KeyValuePair<string, string> which is the key of the dictionary, has carType as key and carModel as the model of the car.
When I run the unit test of it
[TestMethod]
public void DeleteSameCarTypeTest()
{
Dictionary<KeyValuePair<string, string>, car> Dictionary = new Dictionary<KeyValuePair<string, string>, car>();
Dictionary.Add(new KeyValuePair<string, string>("x", "y"), new car());
Dictionary.Add(new KeyValuePair<string, string>("x", "z"), new car());
new Program().deleteAllCarsWithType(Dictionary, "x");
Assert.AreEqual(0, Dictionary.Count);
}
It fails by saying Expected 0 and Actual 2.
When I debug it, I found out that keyValuePairs have the expected two values (which are with carType "x") but when I step into the foreach loop it's not going in and saying that count of keyValuePairs is 0. Then I tried to get the Count of the keyValuePairs which is also coming 0. I don't know what I am doing wrong. Please help me out.