2012-10-27 59 views
0

這是我的實體集合。以Linq語句輸出字典輸出

Dictionary<string, List<Dictionary<string, string>>> EntityCollection = new Dictionary<string, List<Dictionary<string, string>>>(); 

當我在EntityCollection獲得價值,我想對其進行過濾。

var filtered = from entity in EntityCollection 
         where entity.Key == entityId 
         select entity.Value; 

現在我只想要​​。 所以我創建了一個變量:

List<Dictionary<string, string>> entityDetails = new List<Dictionary<string, string>>(); 

我怎麼能投filteredentityDetails

我試過filtered.ToList<Dictionary<string, string>>,但沒有成功。

回答

2

您可以從Dictionary直接獲得,而不是使用LINQ像您一樣的:

List<Dictionary<string, string>> entityDetails = EntityCollection[entityId]; 

或者,如果你想避免第一種方法異常,如果字典裏沒有密鑰

List<Dictionary<string, string>> entityDetails; 
if (EntityCollection.TryGetValue(entityId, out entityDetails)) 
{ 
}