3

How do I modify a value in Dictionary? I want to reassign a value to a value in my dictionary while looping on my dictionary like this:

for (int i = 0; i < dtParams.Count; i++)
{
   dtParams.Values.ElementAt(i).Replace("'", "''");
}

where dtParams is my Dictionary

I want to do some thing like this:

string a = "car";    
a = a.Replace("r","t");
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • possible duplicate of [How to iterate through Dictionary and change values?](http://stackoverflow.com/questions/2260446/how-to-iterate-through-dictionary-and-change-values) – SwDevMan81 Dec 08 '10 at 14:23
  • they are similar but not the same,,, and the solution in How to iterate through Dictionary and change values? doesnot work with me – Anyname Donotcare Dec 08 '10 at 14:53

4 Answers4

8

The string Replace function returns a new modified string, so you'd have to do something like:

foreach (var key in dtParams.Keys.ToArray())
{
   dtParams[key] = dtParams[key].Replace("'", "''");
}

EDIT:

Addressed the collection is modified issue (which I didn't think would occur if you access a key that already exists...)

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
  • Exception:: Collection was modified; enumeration operation may not execute. – Anyname Donotcare Dec 08 '10 at 14:17
  • 1
    try using a for loop or creating a new Dictionary and adding to it instead of removing. You cannot modify the members of the collection in a foreach loop. – Pabuc Dec 08 '10 at 14:18
3

when you use replace , it will return a new string instance , you need to assign the value in the dictionary after replace becaue string is immutable in nature

TalentTuner
  • 17,262
  • 5
  • 38
  • 63
3

You can't do this directly; you cannot modify a collection while you're enumerating it (also, avoid using ElementAt here; this is a LINQ to Objects extension method and is inefficient for iterating over an entire list). You'll have to make a copy of the keys and iterate over that:

foreach(var key in dtParams.Keys.ToList()) // Calling ToList duplicates the list
{
    dtParams[key] = dtParams[key].Replace("'", "''");
}
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • @user418343: If you want help, it would be easier if you'd say *how* it doesn't work (error message, bad output, etc.) rather than just saying "doesnot work also". – Adam Robinson Dec 08 '10 at 15:06
  • @user418343 It should work because that is one of the ways to look at each value in a dictionary collection and modify in the way your looking to do it. You won't be able to use a for loop for this particular task, you could keep track of the values you wanted to change, then modify only those after a single pass through. – Security Hound Dec 08 '10 at 17:49
1

A little lambda will go a long way. ;)

I dropped this into LINQPad and tested it out for you. All the collections have .To[xxx] methods so you can do this quite easily in 1 line.

var dtParams = new Dictionary<string, string>();
dtParams.Add("1", "'");
dtParams.Add("a", "a");
dtParams.Add("b", "b");
dtParams.Add("2", "'");
dtParams.Add("c", "c");
dtParams.Add("d", "d");
dtParams.Add("e", "e");
dtParams.Add("3", "'");

var stuff = dtParams.ToDictionary(o => o.Key, o => o.Value.Replace("'", "''"));
stuff.Dump();
phillip
  • 2,618
  • 19
  • 22