开发者

Is this suitable for Linq GroupBy? [duplicate]

This question already has answers here: LINQ implementation of Cartesian Product with pruning (3 answers) Closed 2 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜