0

我有以下课程:

class XXX
{
    int XXXType{get;set;}
    List<YYY> Children{get;set;}
}
class YYY
{
    int YYYType{get;set;}
}

我想找到属性 XXXType 具有相同值的项目数,并且对于每个组,我想找到其子项对属性 YYYType 具有相同值的项目数。linq to sql 是否支持选择/组中的选择/组?我没有得到任何编译/运行时异常,但 YYYChildren 的长度始终为零。

(from x in XXX).GroupBy(x=>x.XXXType, (key, values) => new {
    XXXType = key,
    Count = values.Count(),
    YYYChildren = (from y in values.SelectMany(z=>z.Children).GroupBy(y=>y.YYYType, (key, values) =>
        new {
             YYYType = key,
             Count = values.Count()
        }
    });
4

1 回答 1

0

像这样的东西...

//样本数据

   var xxxList = new List<XXX> {
            new XXX()
            {
                XXXType = 1,
                Children = new List<YYY>() {
                        new YYY() {YYYType = 1},
                        new YYY() {YYYType = 1},
                        new YYY() {YYYType = 2},
                        new YYY() {YYYType = 2}
                    }
            }
        };

//询问 :

        foreach(var x in xxxList.GroupBy(info => info.XXXType)
                    .Select(group => new { 
                         key = group.Key, 
                         Count = group.Count(),
                         YYYChildren = (from y in xxxList.SelectMany(z=>z.Children).GroupBy(y=>y.YYYType, (key, values) =>
                                        new {
                                             YYYType = key,
                                             Count = values.Count()
                                        }) select y.YYYType)
                    }))
        {
             Console.WriteLine("XXXType :{0}, Number Of XXXTypes :{1}, Number Of YYYChildren :{2}", x.key, x.Count, x.YYYChildren.Count());
        }

结果:XXXType:1,XXXTypes 数量:1,YYYChildren 数量:2

于 2013-10-06T00:25:28.970 回答