germanSharper
germanSharper

Reputation: 971

How can I group by a list of elements?

I run into this issue again and again: how can I group a list of objects by a containing list of other objects?

I have a list of objects of type A and each of these objects has an property (lets call it ListProp) which is a list also. ListProp has elements of the type B. There are multiple elements of type A with identically B-objects in ListProp, but the ListProp property reference differs from element to element. How can I group these A-objects the fastest way, where the B-objects in ListProp are identically?

Sample code:

class Program
{
    static void Main(string[] args)
    {
        var exampleList = new List<A>
        {
            // Should be in first group
            new A { ListProp = new List<B>
            {
                new B { Prop = new C { Number = 0 }},
                new B { Prop = new C { Number = 1 }}
            }},
            // Should be in first group
            new A { ListProp = new List<B>
            {
                new B { Prop = new C { Number = 0 }},
                new B { Prop = new C { Number = 1 }}
            }},
            // Should be in second group
            new A { ListProp = new List<B>
            {
                new B { Prop = new C { Number = 0 }},
                new B { Prop = new C { Number = 1 }},
                new B { Prop = new C { Number = 1 }}
            }},
            // Should be in third group
            new A { ListProp = new List<B>
            {
                new B { Prop = new C { Number = 0 }},
                new B { Prop = new C { Number = 0 }}
            }}
        };

        // Doesn't work because the reference of ListProp is always different
        var groupedExampleList = exampleList.GroupBy(x => x.ListProp);
    }
}

class C
{
    public int Number { get; set; }
    public override bool Equals(object o)
    {
        if (o is C)
            return Number.Equals(((C)o).Number);
        else
            return false;
    }
}

class B
{
    public C Prop { get; set; }
}

class A
{
    public IList<B> ListProp { get; set; }
}

Upvotes: 8

Views: 4122

Answers (3)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

You can implement IEqualityComparer<List<B>> and use it in the other GroupBy overload.

public class ListOfBEqualityComparer : IEqualityComparer<List<B>>
{
    public bool Equals(List<B> x, List<B> y)
    {
        // you can also implement IEqualityComparer<B> and use the overload
        return x.SequenceEqual(y);
    }

    public int GetHashCode(List<B> obj)
    {
        //implementation of List<T> may not work for your situation
        return obj.GetHashCode();
    }
}

Then you can use the overload

var groupedExampleList = exampleList.GroupBy(x => x.ListProp, 
                                             new ListOfBEqualityComparer());

Upvotes: 7

abatishchev
abatishchev

Reputation: 100268

Try this:

GroupBy(x => String.Join(",", x.ListProp));

It will group by 0,1; 0,1; 0,1; 0,1,1; 0,1 accordingly.

Upvotes: 4

RePierre
RePierre

Reputation: 9566

I would approach this the following way:

  1. Associate each child element (in ListProp property) with its parent
  2. Group the parents by children
  3. Project the results

var data = exampleList.SelectMany(a=>a.ListProp.Select(x=>new{Key = x.Prop.Number, Value = a}))
           .GroupBy(x=>x.Key)
           .Select(g=>new {Number = g.Key, Items = g.ToList()});

Upvotes: 0

Related Questions