2016-09-07 77 views
0

值我想訂購的所有元素的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"}, 

              }); 
+0

被定義爲'var y = new KeyValuePair (「1」,1);'例如? – 2016-09-07 13:03:00

+3

請輸入文字,我無法打開圖片鏈接,謝謝 – 2016-09-07 13:12:25

+0

'y'是什麼類型? –

回答

1

您正在尋找的OfTypemethod

valueList.OrderBy(ya => ya.Values.OfType<Dictionary<string,object>>().First(key => key.Equals("propertyToSearchFor"))); 

UPDATE2

var test = valueList.Select(x => new { a=x, b=x["property4"] }) 
       .OrderByDescending(x => x.b).Select(x=>x.a).ToList(); 

的情況下,要管理一個不存在的關鍵(避免除外)

Func<string,Dictionary<string,object>,object> func = (s,x) => { object o = null; x.TryGetValue(s, out o); return o; }; 
    var test = valueList.Select(x => new { a=x, b = func("nonExisting",x)}) 
     .OrderByDescending(x => x.b).Select(x=>x.a).ToList(); 

所以下面訂購您的valueList在更新2(降序)通過的(現有的)property5

var testProperty5Desc = valueList.Select(x => new { a=x, b = func("property5",x)}) 
        .OrderByDescending(x => x.b).Select(x=>x.a).ToList(); 
+0

@ ASfdsa33我需要一個文本代碼示例來產生valueList或者y和然後是valueList。不要發佈圖片,謝謝 – 2016-09-07 13:37:38

+0

不,我得到類似「字典中不存在的密鑰」... – ASfdsa33

+0

@ ASfdsa33再次更新,以避免對於非現有密鑰的異常...我已經用您的valueList在update2中,它正在訂購 – 2016-09-07 14:41:25

0

如果valueListList<T>T是一個Dictionary<string, object>有沒有成員Values,還是我失去了什麼?當前元素ya已經一個Dictionary<string, object>,所以你不需額外需要轉換它:

List<Dictionary<string, object>> valueList = ... 
var result = valueList.OrderBy(ya => ya.Keys.First(key => key.Equals("propertyToSearchFor"))); 
0

在這個例子中,變量yavalueList的元素。 所以這是一個Dictionary<string, object>類型。 所以這行代碼應該工作。

valueList.OrderBy(ya => ya.Values.First(key => key.Equals("propertyToSearchFor"))); 
相關問題