2009-10-10 50 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

相關問題