Home > Net >  How to get all values associated with the same key in a list of dictionaries?
How to get all values associated with the same key in a list of dictionaries?

Time:02-20

I have a Class that looks like this

public class VegetablesPriceNode
{
 public string Country{get;set;}
 public Dictionary<string,string> VegetableDictionary{get;set;}
}  

Then I get each Individual VegetablesPriceNode in the form of a List

List<VegetablesPriceNode> = //I get this object after parsing.

There are same keys in all the VegetableDictionary but for different countries, which I need to show it horizontally.

Veggies   Afghanistan.   Bahrain
-------------------------------------------------------------------------
Onions  | $2.00       |  $1.00  ....
Potatoes| $3.00       |  $1.50  ....
.......

public static void Main()
{
   // I get a List<VegetablesPriceNode> from some place
   // Need to extract all values for the same key from all dictionaries 
   
   // I tried to extract all keys first using 
   var  = vegetablesPriceNodeList.SelectMany(x => x.VegetableDictionary.Select(x => x.Key)).ToHashSet();

   // I chose HashSet because all keys were getting appended and there is no unique.

}

How can I get extract the same key information iterating over all dictionaries for all countries? e.g I want all Onions prices from all countries. so loop through all dictionaries for Onions as the key and get their values?

Is the above way of extracting keys first and then fetching all values for this key is ideal? or is there a better solution?

Since I am using DataTable to store this information the DataTable uses object[] the object I am getting is List

Expected: List of string values which look like

"Onions","$2.00","$3.00"

Sample data:

[{"Country":"Afghanistan","VegetableDictionary":{"Onions":"$1.00","Potatoes":"$1.40"}},{"Country":"Bahrain","VegetableDictionary":{"Onions":"$3.00","Potatoes":"$3.40"}}]

CodePudding user response:

I don't know exactly what is your desired output.

I believe something similar:

[["Onions","$1.00","$3.00"],["Potatoes","$1.40","$3.40"]]

I made an example, I hope it could be useful in your use case:

https://dotnetfiddle.net/RPFZen

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

public class VegetablesPriceNode
{
    public string Country { get; set; }
    public Dictionary<string, string> VegetableDictionary { get; set; }
}

public class Program
{
    public static void Main()
    {
        var json = "[{\"Country\":\"Afghanistan\",\"VegetableDictionary\":{\"Onions\":\"$1.00\",\"Potatoes\":\"$1.40\"}},{\"Country\":\"Bahrain\",\"VegetableDictionary\":{\"Onions\":\"$3.00\",\"Potatoes\":\"$3.40\"}}]";

        var vegetablesPriceNodeList = JsonConvert.DeserializeObject<List<VegetablesPriceNode>>(json);

        var obj = vegetablesPriceNodeList
            .SelectMany(x =>
                x.VegetableDictionary
                    .Select(x => x.Key)
            )
            .Distinct()
            .Select((k) =>
                vegetablesPriceNodeList
                    .Select(v => v.VegetableDictionary[k])
                    .Prepend(k)
                    .ToArray()
            ).ToArray();

        var output = JsonConvert.SerializeObject(obj);
        
        Console.WriteLine(output);
    }
}
  • Related