tags:

views:

51

answers:

1

Hi is there anyway to write somthing like this

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

into this

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

so that i can group by " group i by i.Number, i.CurrentStatus"

+7  A: 

Group by an anonymous type:

        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()
                };
Quartermeister
Use `Reported = grp.Key.CurrentStatus` instead of `Reported = grp.Select(x => x.CurrentStatus).First()`.
Jaroslav Jandek
@Jaroslav: Good point! I updated the answer with your fix.
Quartermeister
Thanks its works great!
Tan