Q:

Getting the MAX of several SUMs with LINQ

I have a table called UserActivity that has ActivityId and Points columns. Entries are not unique, so there are many rows that have the same ActivityId. What I want is to sum up all the points for each activity, then return the ActivityId and Points for the activity that has the most points. So, in SQL, something like this (untested):
SELECT ActivityPoints.ActivityId
FROM (
    SELECT ActivityId, SUM(Points) AS TotalPoints
    FROM db.UserActivity
    GROUP BY ActivityId
) AS ActivityPoints
HAVING ActivityPoints.TotalPoints = MAX(ActivityPoints.TotalPoints)

How would I achieve this in LINQ? I'm brand new to it, so I'm not at all sure what I'm doing.

All great answers, everyone! I ended up learning a little bit from each of them. Thanks.

A:

Are you trying to create your own StackOverflow ? :-)
IEnumerable<UserActivity> UserActivities = LoadUserActivities();
int maxTotalPointsActivityId = 
    UserActivities
    .GroupBy(ua => ua.ActivityId)
    .OrderByDescending(uaGp => uaGp.Sum(ua => ua.Points))
    .First()
    .Key;

