0

I have a Dictionary :

      Dictionary<Project,List<User>> dictionary,

every Project has a group of Users.

I want to retrieve organize this data such that all the projects with similar users are arranged into this kind of data structure :

      List<Tuple<List<Project>,List<User>>>

I don't even know where to start. I've been fighting with this for days.

Attilah
  • 17,632
  • 38
  • 139
  • 202

2 Answers2

0

Your best best would be to flatten the contents of dictionary and query against that.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
0

The lists of users are the key by which you should group. In the GroupBy() you could use a comparer like this one:

public class ListComparer<T> : IEqualityComparer<List<T>>
    where T : IComparable<T>
{
    public bool Equals(List<T> x, List<T> y)
    {
        return x.OrderBy(u => u).SequenceEqual(y.OrderBy(u => u));
    }

    public int GetHashCode(List<T> obj)
    {
        return obj.GetHashCode();
    }
}

BTW: The comparer is on Lists and not on IEnumerables, otherwise for each comparison the compared objects should be enumerated over and over.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • 1
    You might want to follow [this thread](http://stackoverflow.com/questions/6948019/linq-implementation-of-cartesian-product-with-pruning) for a more robust comparer. – Gert Arnold Aug 04 '11 at 21:45
  • I'd like to delete this answer because it's not good. But it's accepted so it can't be deleted. The GetHashCode method doesn't make sense. It should match the `Equals` method but it doesn't. – Gert Arnold Feb 22 '21 at 07:17