1

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.

14
  • 4
    Define duplicate dictionary first. Commented Jul 24, 2015 at 7:39
  • 1
    User, your update won't compile, there isn't a groupedItems and newItem is used outside of its scope Commented Jul 24, 2015 at 7:41
  • 2
    I 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 ....? Commented Jul 24, 2015 at 7:42
  • 1
    @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 Commented Jul 24, 2015 at 7:49
  • 1
    @user2818430 Can you post your linq GroupBy code? Commented Jul 24, 2015 at 8:01

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
}

1 Comment

What is "items" referring to here?
0

You can implement your own EqualityComparer to determine if two dictionaries are equal:

class EqualityComparer<Dictionary<string, string>> : IEqualityComparer<Dictionary<string, string>>
{

    public bool Equals(Dictionary<string, string> x, Dictionary<string, string> y)
    {
        // your code here
    }

    public int GetHashCode(Dictionary<string, string> obj)
    {
        // your code here
    }
}

Now you may use this comparer within a check for existance of a new item:

foreach (var g in groupedItems)
{
    Dictionary<string, string> newItem = new Dictionary<string, string>();

    foreach(var item in g) 
    {
        newItem.Add("name", item.Name);
        newItem.Add("value", item.Value);    
    }
    if (!items.Contains(newItem, new EqualityComparer()) items.Add(newItem);
}

Thus there is no need to create a new implementation of Dictionary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.