简体   繁体   English

使用Enumerable.Range()填充字典时出现问题

[英]Problem in populating a dictionary using 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 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. 无法从用法推断出方法'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable,System.Func)'的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

Any idea? 任何的想法?

Using C#3.0 使用C#3.0

Thanks 谢谢

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

Thanks I got the answer 谢谢,我得到了答案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM