Problem filling dictionary with Enumerable.Range ()

If i do

for (int i = 0; i < appSettings.Count; i++)
{
   string key = appSettings.Keys[i];
   euFileDictionary.Add(key, appSettings[i]);
}

It is working fine.

When I try to use the same using

Enumerable.Range(0, appSettings.Count).Select(i =>
{
   string Key = appSettings.Keys[i];
   string Value = appSettings[i];
   euFileDictionary.Add(Key, Value);
}).ToDictionary<string,string>();

I get a compile time error

Type arguments to the method "System.Linq.Enumerable.Select (System.Collections.Generic.IEnumerable, System.Func)" cannot be taken out of use. Try explicitly specifying type arguments.

Any idea?

Using C # 3.0

thank

+3
source share
3 answers
Enumerable.Range(0, appSettings.Count).Select(i =>
new  
{   
   Key = appSettings.Keys[i],
   Value = appSettings[i]
})
.ToDictionary(x => x.Key, x => x.Value);
+3
source
Enumerable.Range(0, appSettings.Count)
          .ToDictionary(
              i => appSettings.Keys[i],
              i => appSettings[i]);
+2
source

Thank you, I got an answer.

Enumerable.Range(0, appSettings.Count).ToList().ForEach(i =>
{ 
   euFileDictionary.Add(appSettings.Keys[i], appSettings[i]);
});
0
source

Source: https://habr.com/ru/post/1743802/


All Articles