2012-11-28 29 views
1

我試圖使用下面的代碼把一個IEnumerable<KeyValuePair<string, object>>ILookup<string, object>IEnumerable的<T> .ToLookup <TKEY的,TValue>中

var list = new List<KeyValuePair<string, object>>() 
{ 
    new KeyValuePair<string, object>("London", null), 
    new KeyValuePair<string, object>("London", null), 
    new KeyValuePair<string, object>("London", null), 
    new KeyValuePair<string, object>("Sydney", null) 
}; 

var lookup = list.ToLookup<string, object>(a => a.Key); 

但是,編譯器與抱怨:

實例參數:不能從 'System.Collections.Generic.List>' 到 'System.Collections.Generic.IEnumerable'

'System.Collections.Generic.List>' 不包含關於 'ToLookup' 和最好延伸 方法重載 「System.Linq.Enumerable.ToLookup(系統的定義。 Collections.Generic.IEnumerable, System.Func)」有一些無效參數

無法從'lambda表達式'轉換爲'System.Func'

我在做什麼錯誤的lambda表達式?

回答

4

只是刪除<string, object>的類型自動推斷:

var lookup = list.ToLookup(a => a.Key); 

,因爲它確實應該是:

var lookup = list.ToLookup<KeyValuePair<string, object>, string>(a => a.Key); 
+0

啊哈!謝謝,我從文檔沒有意識到第一個參數是* Source *類型 – Darbio

相關問題