Is this suitable for Linq GroupBy? [duplicate]
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 fight开发者_如何学Pythoning with this for days.
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.
Your best best would be to flatten the contents of dictionary
and query against that.
精彩评论