5

How do I alter the contents of an IDictionary using C# 3.0 (Linq, Linq extensions) ?

var enumerable = new int [] { 1, 2};
var dictionary = enumerable.ToDictionary(a=>a,a=>0);
//some code
//now I want to change all values to 1 without recreating the dictionary
//how it is done?
Michael Meadows
  • 27,796
  • 4
  • 47
  • 63
Jader Dias
  • 88,211
  • 155
  • 421
  • 625

3 Answers3

4

LINQ is a query dialect - it isn't directly a mutation language.

To change the values of an existing dictionary, foreach is probably your friend:

foreach(int key in dictionary.Keys) {
    dictionary[key] = 1;
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
4

This is not nearly as clear as other ways, but it should work fine:

dictionary.Keys.ToList().ForEach(i => dictionary[i] = 0);

My other alternative would have been to make a ForEach extension method similar to this:

public static class MyExtensions
{
    public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
    {
        foreach (var item in items)
        {
            action(item);
        }
    }
}

Then use it like this:

dictionary.ForEach(kvp => kvp.Value = 0);

This won't work in this case though, as Value cannot be assigned to.

Ryan Versaw
  • 6,417
  • 3
  • 30
  • 31
  • So basically, I would never use the first piece of code in reality. The extension method can be great, but as Value cannot be assigned to, dictionaries don't allow for much. – Ryan Versaw Jun 04 '09 at 21:19
  • 1
    Not as clear *and* less efficient (creates a cloned list of the keys, and does 2 iterations instead of 1)... a winner! – Marc Gravell Jun 04 '09 at 22:10
2
foreach (var item in dictionary.Keys)
    dictionary[item] = 1;

I wonder why you might a need doing such a thing, though.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789