Getting keys and values ​​from IEnumerable <Dictionary <string, object >>

I have an IEnumerable < Dictionary < string, object > > .

I want to get "aaavalue" "bbbvalue" "cccvalue" "dddvalue" in an array.

Sample data:

 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 */ 

I know this is possible with Reflection or LINQ. But I could not do it.

Please, help..

Answer:

 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. } 
+4
source share
1 answer

Try:

 IEnumerable<object> values = testData.SelectMany(x => x.Values); 
+7
source

Source: https://habr.com/ru/post/1434193/


All Articles