5
votes

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 am trying the same thing 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 am getting a compile time error

The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Any idea?

Using C#3.0

Thanks

3
What is the type of appSettings ? - leppie
Seems you have figured something out, but... Your second code snippet doesn't make a whole lot of sense. Was it purely an academic exercise? - Thorarin

3 Answers

4
votes
Enumerable.Range(0, appSettings.Count).Select(i =>
new  
{   
   Key = appSettings.Keys[i],
   Value = appSettings[i]
})
.ToDictionary(x => x.Key, x => x.Value);
3
votes
Enumerable.Range(0, appSettings.Count)
          .ToDictionary(
              i => appSettings.Keys[i],
              i => appSettings[i]);
0
votes

Thanks I got the answer

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