C# Linq Grouping

前端 未结 2 1168
面向向阳花
面向向阳花 2021-01-01 16:40

I\'m experimenting with Linq and am having trouble figuring out grouping. I\'ve gone through several tutorials but for some reason can\'t figure this out.

As an exam

2条回答
  •  时光取名叫无心
    2021-01-01 17:00

    Actually, although Thomas' code will work, it is more succint to use a lambda expression:

    var totals =
    from s in sites
    group s by s.SiteID into grouped
    select new
    {
        SiteID = grouped.Key,
        Last30Sum = grouped.Sum( s => s.Last30 )
    };
    

    which uses the Sum extension method without the need for a nested LINQ operation.

    as per the LINQ 101 examples - http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#sumGrouped

提交回复
热议问题