值我想訂購的所有元素的LINQ爲「列表<字典<串,KeyValuePair <串,對象>>>」命令由ASC
這是我能遍歷它的方式:
List<Dictionary<string, object>> valueList = ((IEnumerable<object>)y.Value).Select(x => (Dictionary<string, object>)x).ToList();
foreach (Dictionary<string, object> dict in valueList)
{
foreach (KeyValuePair<string, object> item in dict)
{
}
}
我有形成LINQ表達,這將所有的值以便在特定的鍵巨大的問題。(例如,我有特殊的鍵和值I要重新排序的整個數據源)
valueList.OrderBy(ya => (ya.Values as List<Dictionary<string, KeyValuePair<string, object>>>).Keys.First(key => key.Equals("propertyToSearchFor")));
我得到:
無法將類型 'System.Collections.Generic.Dictionary.ValueCollection' 到 'System.Collections.Generic.List >>' 通過引用轉換,裝箱轉換,取消裝箱轉換 , 包裝轉換,或空類型轉換
應該改爲什麼?
修訂版1個
謝謝回答,這是我使用http://i63.tinypic.com/2d0mhb9.png的數據的一個例子。根據關鍵字「propertyToSearchFor」(看看截圖:「modell」,「marke」或...,「jan」),我有我的valueList
,我需要重新排序。例如,valueList[0]
包含某種數據集,它在valueList[1]
中具有相同的密鑰,但valueList[1]
中的值與valueList[0]
中的值不同。
我需要通過「modell」命令數據資源,它應該遍歷valueList[...]
中的所有元素,並根據modell的值對該列表重新排序。
更新2
這裏是東西複製和粘貼:)
List<Dictionary<string, object>> valueList = new List<Dictionary<string, object>>();
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 34.0f},
{"property5", 5.0d},
{"property6", 'c'},
{"property7", "xtest"},
{"property8", "gtest"},
{"property9", "jtest"},
{"property10", "1ptest"},
{"property11", "atest"},
{"property12", "test"},
{"property13", "ätest"},
{"property14", "test"},
{"property15", "ztest"},
});
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 342.0f},
{"property5", 25.0d},
{"property6", 'h'},
{"property7", "1xtest"},
{"property8", "gtest"},
{"property9", "1jtest"},
{"property10", "1ptest"},
{"property11", "atest"},
{"property12", "1test"},
{"property13", "1ätest"},
{"property14", "test"},
{"property15", "ztest"},
});
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 344.0f},
{"property5", 5.0d},
{"property6", 'z'},
{"property7", "xtest"},
{"property8", "gt213est"},
{"property9", "jtest"},
{"property10", "2311ptest"},
{"property11", "21atest"},
{"property12", "321test"},
{"property13", "231ätest"},
{"property14", "31test"},
{"property15", "z231test"},
});
valueList.Add(new Dictionary<string, object>()
{
{"property1", "test"},
{"property2", null},
{"property3", new Object()},
{"property4", 3.0f},
{"property5", 500.0d},
{"property6", 'z'},
{"property7", "xtest"},
{"property8", "gstest"},
{"property9", "jtest"},
{"property10", "1pstest"},
{"property11", "atsest"},
{"property12", "test"},
{"property13", "ätsest"},
{"property14", "tesst"},
{"property15", "ztsest"},
});
被定義爲'var y = new KeyValuePair(「1」,1);'例如? –
2016-09-07 13:03:00
請輸入文字,我無法打開圖片鏈接,謝謝 – 2016-09-07 13:12:25
'y'是什麼類型? –