8

我一次又一次地遇到这个问题:如何通过包含其他对象的列表对对象列表进行分组?

我有一个类型对象的列表,A每个对象都有一个属性(让我们称之为ListProp),它也是一个列表。ListProp具有类型的元素B。有多个类型元素A具有相同的B-objects in ListProp,但ListProp属性引用因元素而异。如何A以最快的方式对这些对象进行分组,其中的B对象ListProp是相同的?

示例代码:

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

3 回答 3

6

您可以IEqualityComparer<List<B>>在其他 GroupBy 重载中实现和使用它。

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

然后你可以使用重载

var groupedExampleList = exampleList.GroupBy(x => x.ListProp, 
                                             new ListOfBEqualityComparer());
于 2012-04-20T11:09:31.840 回答
4

试试这个:

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

它将0,1; 0,1; 0,1; 0,1,1; 0,1相应地分组。

于 2012-04-20T11:09:30.043 回答
0

我将通过以下方式解决此问题:

  1. 将每个子元素(在 ListProp 属性中)与其父元素关联
  2. 按孩子分组父母
  3. 投影结果

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()});
于 2012-04-20T11:23:11.113 回答