I'm receiving time entries from an external API and store them in a collection of type
public sealed record TimeEntry
{
public string ActivityName { get; }
public float HoursSpent { get; }
}
I want to group the entries by the activity, sum the hours spent and get the percentage compared to the other activities.
Given the example that group1 has 23 and group2 77 hours group 1 should have a percentage of 23% and group 2 77% because there is a total of 100 hours.
I started with this
var activitiesWithSpentHours = timeEntries
.GroupBy(timeEntry => timeEntry.ActivityName)
.Select(activityGroup => new
{
Activity = activityGroup.Key,
HoursSpent = activityGroup.Sum(timeEntry => timeEntry.HoursSpent)
Percentage = 0 /* calculate here */
});
How can I calculate the percentage and add it to the return type? I have to compare the current item with the other ones in the whole list. Does Linq provide any functionality?