LINQ集团有两个声明

时间:2010-07-02 13:23:29

标签: c# linq

嗨,无论如何要写这样的东西

            var r = from i in myList
                group i by i.Number
                    into grp
                    select new
                    {
                        Reported = grp.Select(x => x.CurrentStatus).First(),
                        Number = grp.Key,
                        Sum = grp.Sum(x => x.Details[0].Quantity),
                        Name = grp.Select(x => x.Name).First(),
                        Details = grp.Select(x => x.Details).First(),
                        Descriptions = grp.Select(x => x.Descriptions).First(),
                        AssignmentId = grp.Select(x => x.AssignmentId).First(),
                        Listor = grp.Select(x => x.Number).Count()
                    };

进入这个

            var r = from i in myList
                group i by i.Number, i.CurrentStatus
                    into grp
                    select new
                    {
                        Reported = grp.Select(x => x.CurrentStatus).First(),
                        Number = grp.Key,
                        Sum = grp.Sum(x => x.Details[0].Quantity),
                        Name = grp.Select(x => x.Name).First(),
                        Details = grp.Select(x => x.Details).First(),
                        Descriptions = grp.Select(x => x.Descriptions).First(),
                        AssignmentId = grp.Select(x => x.AssignmentId).First(),
                        Listor = grp.Select(x => x.Number).Count()
                    };

这样我就可以按“i.Number,i.Current Status”分组。

1 个答案:

答案 0 :(得分:10)

按匿名类型分组:

        var r = from i in myList
            group i by new { i.Number, i.CurrentStatus }
                into grp
                select new
                {
                    Reported = grp.Key.CurrentStatus,
                    Number = grp.Key.Number,
                    Sum = grp.Sum(x => x.Details[0].Quantity),
                    Name = grp.Select(x => x.Name).First(),
                    Details = grp.Select(x => x.Details).First(),
                    Descriptions = grp.Select(x => x.Descriptions).First(),
                    AssignmentId = grp.Select(x => x.AssignmentId).First(),
                    Listor = grp.Select(x => x.Number).Count()
                };