简体   繁体   中英

How to select many distinct elements on List<Dictionary<string, string>>?

I would like to select entries in a list of dictionaries in order to create a new dictionary containing only distinct items necessary for me.

For example, starting with this list:

List<Dictionary<string, string>> test = new List<Dictionary<string, string>>{            
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret B"}, {"idtGroupe", "idtLivretB"}, {"variables", "test2"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret B"}, {"idtGroupe", "idtLivretB"}, {"variables", "test2"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret B"}, {"idtGroupe", "idtLivretB"}, {"variables", "test2"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
    new Dictionary<string, string>{{"labelGroup", "Livret A"}, {"idtGroupe", "idtLivretA"}, {"variables", "test1"}},
};

I would like to have a new dictionary without the key "variables" and that contains :

{"labelGroup", "Livret A"},
{"idtGroupe", "idtLivretA"},
{"labelGroup", "Livret B"},
{"idtGroupe", "idtLivretB"}

You can use Distinct() method. You have to iterate on your list of dictionary for each key. Your output has to be list of dictionary otherwise you will encounter duplicate key issue.

            var distinctDictionary = new List<Dictionary<string, string>>();

            foreach (var keyItem in test.First().Keys)
            {
                if (keyItem == "variables")
                    continue;

                var tempDistinct = (from t in test select t[keyItem]).Distinct();

                foreach (var distinctItem in tempDistinct)
                {
                    distinctDictionary.Add(new Dictionary<string, string>() { { keyItem, distinctItem } });
                }
            }

If your test dictionary is empty, First() method will throw an exception. You may want to check if it is empty or not before iteration.

Answer above is good, but you can do that with simple one liner.

  var distincts = test.SelectMany(x => x).Where(x=> x.Key != "variables").Distinct(); 

SelectMany() will select all items from you dictionaries and join to one Dictionary

Distinct() - easy one, this will select distinct items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM