In the query below I do calculate the combined value of all football teams within league 1. The solution I've came up with, though it works, seems rather bulky.
I would like to find a better approach i.e. a one lined linq query like so:
footballteams.Where(x => x.League == 1).Sum(x => x.Networth);
My current code:
List<IGrouping<int, FootballTeam>> footballTeams = context.FootballTeam
.GetQueryable()
.GroupBy(x => x.TeamName)
.ToList();
var prem = footballTeams.Where(x => x.League == 1).ToList();
var totalWealth = 0;
foreach (var team in prem)
{
foreach(var singleteam in team)
{
totalWealth = totalWealth + singleteam.networth;
}
}
networth? Wouldn't it make more sense to get the teams as aList<FootballTeam>, then from this list you can group or you can sum, whichever you like? Also, how can you run.Where(x => x.League == 1)on anIGrouping? Those objects don't have a.Leagueproperty.