我對csharp和linq很新,所以在簡單的轉換問題上遇到了一些麻煩。轉換linq結果到字典<string, int>
我有一個簡單的對象 -
public class AskQuestionFormView
{
public string QuestionText { get; set; }
public Dictionary<string,int> Location { get; set; }
}
和LINQ查詢
AskQuestionFormView aqfv = new AskQuestionFormView();
var result = from c in db.Countries
.ToDictionary(c => c.LocationName.ToString(),
c=>c.ID)
select c;
aqfv.Location = (Dictionary<string,int>)result;
,但我發現了以下錯誤 -
System.InvalidCastException:無法投對象的類型'WhereSelectEnumerableIterator`2 [System.Collections.Generic.KeyValuePair`2 [System.String,System.Int32],System.Collections.Gen eric.KeyValuePair`2 [System.String,System.Int32]]'鍵入'System.Collections.Generic.Dictionary`2 [System.String,System.Int32]'。
我在做什麼錯?
在此先感謝。
這是絕對完美的。非常感謝。 – Finnnn 2011-02-02 20:17:55