2016-09-07 66 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"))); 
相关问题