[英]How to collect values for all the keys from a Dictionary<string,object> in a List
[英]Getting Keys and Values from IEnumerable<Dictionary<string, object>>
我有一个IEnumerable < Dictionary < string, object > >
对象。
我想在数组中得到“aaavalue”“bbbvalue”“cccvalue”“dddvalue”。
样本数据:
IEnumerable<Dictionary<string, object>> testData = new IEnumerable<Dictionary<string, object>>();
/* Values in testData will be like
[0] -
aaa - aaaValue (Key, Value)
bbb - bbbValue
ccc - cccValue
ddd - dddValue
[1] -
aaa - aaaValue (Key, Value)
bbb - bbbValue
ccc - cccValue
ddd - dddValue
and so on */
我知道可以使用Reflection或LINQ。 但我无法弥补。
请帮忙..
回答:
IEnumerable<Dictionary<string, object>> enumerable = testData as List<Dictionary<string, object>> ?? testData .ToList();
foreach (Dictionary<string, object> objects in enumerable)
{
IEnumerable<object> values = objects.Select(x => x.Value); // To get the values.
IEnumerable<string> keys = objects.Select(x => x.Key); // To get the keys.
}
试试:
IEnumerable<object> values = testData.SelectMany(x => x.Values);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.