GroupBy的ObservableCollection项的值

时间:2017-11-26 12:19:12

标签: c# linq

我有以下代码:

    class MyClassDetails
    {
        public int Value;
    }

    class MyClass
    {
        public MyClass()
        {
            moreDetails = new ObservableCollection<MyClassDetails>();
        }
        public string Name;
        public ObservableCollection<MyClassDetails> moreDetails;
    }

    static void Main(string[] args)
    {
        var myList = new List<MyClass>();

        var myClass1 = new MyClass();
        myClass1.Name = "Class1";
        myClass1.moreDetails.Add(new MyClassDetails() { Value = 1 });
        myClass1.moreDetails.Add(new MyClassDetails() { Value = 2 });
        myClass1.moreDetails.Add(new MyClassDetails() { Value = 4 });
        myClass1.moreDetails.Add(new MyClassDetails() { Value = 16 });

        var myClass2 = new MyClass();
        myClass2.Name = "Class2";
        myClass2.moreDetails.Add(new MyClassDetails() { Value = 1 });
        myClass2.moreDetails.Add(new MyClassDetails() { Value = 2 });
        myClass2.moreDetails.Add(new MyClassDetails() { Value = 4 });
        myClass2.moreDetails.Add(new MyClassDetails() { Value = 16 });

        myList.Add(myClass1);
        myList.Add(myClass2);


        var groups1 = myList.GroupBy(myClass => myClass.Name);//This groups by class Name
        foreach(var group in groups1)
        {
            var key = group.Key;//key is e.g 'Class1'/'Class2'
        }

       //does not work??
        var groups2 = myList.GroupBy(myClass => myClass.moreDetails)
                            .Select(g => new { Value = g.Key, Count = g.ToList().Distinct().Count() });
        foreach (var group in groups2)
        {
            //WTF??
        }
    }

如何将原始myList分组到Key为Value的组中,例如1,2,4,16 ans我知道每个值的总数。 因此,对于以下内容,我最终会得到4组:

Group 1 Key=1 Value=2
Group 2 Key=2 Value=2
Group 3 Key=4 Value=2
Group 4 Key=16 Value=2

所以基本上它有多少不同的&#39;价值是什么,以及myList中每个的数量

更新问题 我现在已经将以下ID添加到MyClassDetails类。

class MyClassDetails
{
    public string ID;  <--Added ID
    public int Value;
}

var myList = new List<MyClass>();

var myClass1 = new MyClass();
myClass1.Name = "Class1";
myClass1.moreDetails.Add(new MyClassDetails() { Value = 1, ID="ONE" });
myClass1.moreDetails.Add(new MyClassDetails() { Value = 2, ID = "TWO" });
myClass1.moreDetails.Add(new MyClassDetails() { Value = 4, ID = "ONE" });
myClass1.moreDetails.Add(new MyClassDetails() { Value = 8, ID = "TWO" });
myClass1.moreDetails.Add(new MyClassDetails() { Value = 16, ID = "ONE" });

var myClass2 = new MyClass();
myClass2.Name = "Class2";
myClass2.moreDetails.Add(new MyClassDetails() { Value = 1, ID = "TWO" });
myClass2.moreDetails.Add(new MyClassDetails() { Value = 2, ID = "ONE" });
myClass2.moreDetails.Add(new MyClassDetails() { Value = 4, ID = "ONE" });
myClass2.moreDetails.Add(new MyClassDetails() { Value = 8, ID = "TWO" });
myClass2.moreDetails.Add(new MyClassDetails() { Value = 54, ID = "ONE" });

myList.Add(myClass1);
myList.Add(myClass2);

如何修改答案中给出的查询,以便我只按ID计算不同的值。

因此对于上面的值= 4和值= 8只有count = 1,因为它们具有相同的ID值。

1 个答案:

答案 0 :(得分:2)

您需要先在多个对象中选择所需的详细信息,然后对这些对象进行分组

var groups2 = myList.SelectMany(myClass => myClass.moreDetails, (myClass, details) => new { details.Value, myClass.moreDetails} )
                    .GroupBy(myClass => myClass.Value)
                    .Select(g => new { Value = g.Key, Count = g.ToList().Distinct().Count() });