c# List<Dictionary<string,string>> add unique dictionaries
Solution 1
First thing that comes to mind would be to create your own class which extends
Dictionary<string, string>
and implement your own version of GetHashCode()
and Equals
:
public class MyDictionary : Dictionary<string, string>
{
public override int GetHashCode()
{
...
}
public override bool Equals(object source)
{
...
}
}
Within the Equals
you implement your equality mechanism, and in GetHashCode
you implement a mechanism which yields the same numeric value for two dictionaries which are the same, according to your equality criteria.
Then, instead of a List<Dictionary<string, string>>
, you use a HashSet<MyDictionary>
. Since sets do not allow duplicates, you should end up with a collection of unique dictionary collections.
Solution 2
I solved this in this way:
I created a new dictionary:
Dictionary<string, string> control = new Dictionary<string, string>();
And then I just do like this:
Dictionary<string, string> newItem = new Dictionary<string, string>();
newItem.Add("name", item.Name);
newItem.Add("value", item.Value);
if (!control.ContainsKey(item.Name))
{
control.Add(item.Name);
items.Add(newItem);
}
user2818430
Updated on June 05, 2022Comments
-
user2818430 almost 2 years
I have a list of dictionary:
List<Dictionary<string, string>> items = new List<Dictionary<string, string>>(); foreach (var group in groupedItems) { foreach (var item in group) { Dictionary<string, string> newItem = new Dictionary<string, string>(); newItem.Add("name", item.Name); newItem.Add("value", item.Value); } } items.Add(newItem);
Basically when I loop through the grouped items, I create a Dictionary where the key is the item.Name and value is item.Value. In a grouped case, this will result in duplicate dictionaries to the list.
How can I avoid adding duplicate Dictionary to this List?
I have a foreach loop and I want to add some items once.
-
Tim Schmelter almost 9 yearsDefine duplicate dictionary first.
-
user2818430 almost 9 yearsI made an edit to the question
-
mboldt almost 9 yearsDoes it work if you use a HashSet<Dictionarystring, string>> instead of a List?
-
Sayse almost 9 yearsUser, your update won't compile, there isn't a
groupedItems
andnewItem
is used outside of its scope -
Tim Schmelter almost 9 yearsI know but it doesnt explain your definition of duplicate dictionaries. Is a dictionary a duplicate if any of the keys is in one of the other dictonary's keys? Or must all keys be equal or must all keys and all values equal or must it be the same reference ....?
-
user2818430 almost 9 years@mboldt: HashSet doesn't work.
-
Thomas Ayoub almost 9 yearsThis code doesn't even compile
-
user2818430 almost 9 yearsOf course it doesn't compile because the groupedItems is Linq grouped by query which is not present in the above code.
-
vc 74 almost 9 years@user2818430 But in this case item is an IGrouping<string, string> so it's got a Key and implements IEnumerable<string> but it doesn't have a Name or a Value
-
user2818430 almost 9 years@vc: True that. My mistake. I made a change to the question. So there are 2 foreach. One for each group in groupedItems and second for each item in group.
-
MakePeaceGreatAgain almost 9 yearsWhat is
gropuedItems
? -
vc 74 almost 9 years@user2818430 OK I see, so what you're actually after is groupedItems.Select(group => group.ToDictionary(item => item.Name, item => item.Value)).ToList() which produces a dictionary per group entry. But then what do you mean by duplicate dictionary?
-
user2818430 almost 9 years@vc74: Exactly it produces the dictionary per group entry. Now foreach group the same dictionary is produced and inserted to the list which results in duplicate dictionary within the list.
-
vc 74 almost 9 years@user2818430 Can you post your linq GroupBy code?
-
-
Ram about 4 yearsWhat is "items" referring to here?