我想订购所有元素
这是我可以迭代它的方式:
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表达式方面遇到了很大的问题,它会将所有值都排序到一个特定的键。(例如,我有特殊的键,我想重新排序整个数据源的值)
valueList.OrderBy(ya => (ya.Values as List<Dictionary<string, KeyValuePair<string, object>>>).Keys.First(key => key.Equals("propertyToSearchFor")));
我明白了:
无法转换类型 &#39; System.Collections.Generic.Dictionary.ValueCollection&#39; 至 &#39;&System.Collections.Generic.List GT;&GT;&#39; 通过引用转换,装箱转换,拆箱转换, 包装转换或空类型转换
它应该是什么?
更新1
感谢您的回答,这是我使用http://i63.tinypic.com/2d0mhb9.png数据的一个示例。
我有valueList
,我需要重新排序,具体取决于键&#34; propertyToSearchFor&#34;(看一下屏幕截图:&#34; modell&#34;,&#34 ; marke&#34;或......,&#34; jan&#34;)。例如,valueList[0]
包含某种数据集,其valueList[1]
中具有相同的键,但valueList[1]
中的值与valueList[0]
中的值不同。
我需要通过&#34; modell&#34;来订购数据资源。它应该遍历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 :(得分:1)
您是否在寻找OfType
method?
valueList.OrderBy(ya => ya.Values.OfType<Dictionary<string,object>>().First(key => key.Equals("propertyToSearchFor")));
更新后
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
property5
进行排序
var testProperty5Desc = valueList.Select(x => new { a=x, b = func("property5",x)})
.OrderByDescending(x => x.b).Select(x=>x.a).ToList();
答案 1 :(得分:0)
如果float
是valueList
而List<T>
是T
,则没有成员Dictionary<string, object>
,或者我错过了什么?当前元素Values
已经 一个ya
,因此您无需投出它:
Dictionary<string, object>
答案 2 :(得分:0)
在示例中,变量ya
是valueList
的元素。
所以它是Dictionary<string, object>
类型。
所以这行代码应该可行。
valueList.OrderBy(ya => ya.Values.First(key => key.Equals("propertyToSearchFor")));