C # Increase by 1 the value of a Dictionaryint, int with LINQ

1

I have the following dictionary

Dictionary<int, int> datos

Which contains the following key-values:

[0, 2015]
[1, 2016]
[2, 2017]

My goal is to increase the value by 1 ( with LINQ ) so that it stays like this:

[0, 2016]
[1, 2017]
[2, 2018]

Thanks in advance.

    
asked by Corpex 20.07.2017 в 11:38
source

1 answer

3

As I say in my comment, LINQ is a query language, not a modification language. In your case, what you want is very simple as long as you understand that the LINQ query will return a new collection. Then you can assign the new collection to the old one:

var nuevosDatos = datos.ToDictionary(x => x.Key, y=> y.Value + 1);
datos = nuevosDatos;

Or directly:

datos = datos.ToDictionary(x => x.Key, y=> y.Value + 1);
    
answered by 20.07.2017 / 11:50
source