2009-10-10 51 views
0

我有以下代码分割上换行符的字符串并将其转换为一个字典用于进一步处理:一些不寻常的错误尝试字符串[]转换为一个字典<短,字符串>当

 string[] splitProgram = program.Split(Environment.NewLine.ToCharArray()); 
     short i = 0; 
     Dictionary<short, string> programDictionary = splitProgram.ToDictionary<short, string>((value) => i++); 

什么是奇怪的是我上三线以下错误:

Error 1 Instance argument: cannot convert from 'string[]' to 'System.Collections.Generic.IEnumerable<short>' 
Error 2 'string[]' does not contain a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' has some invalid arguments 
Error 3 Argument '2': cannot convert from 'lambda expression' to 'System.Func<short,string>' 

我绝对难倒这并不能弄明白。有人可以帮忙吗?

回答

3

您是否注意到源和密钥在ToDictionary<TSource,TKey>的订单?

你可以尝试:

//untested 
... = splitProgram.ToDictionary<string, short>((value) => i++); 
+0

没有注意到,在所有的,谢谢你啊!虽然如此,但我现在有点疲惫...... – RCIX 2009-10-10 09:54:59

+1

这就是为什么最好将它留给编译器:splitProgram.ToDictionary(value => i ++); – 2009-10-10 09:57:55

相关问题