How can I employ Linq to select Top value from each group
when I have a code segment like :
var teams = new Team[]
{
new Team{PlayerName=\"Ricky\
This will require you to group by team name then select the max score.
The only tricky part is getting the corresponding player, but its not too bad. Just select the player with the max score. Of coarse, if its possible for more than one player to have identical scores do this using the First() function as shown below rather than the Single() function.
var x =
from t in teams
group t by t.TeamName into groupedT
select new
{
TeamName = groupedT.Key,
MaxScore = groupedT.Max(gt => gt.PlayerScore),
MaxPlayer = groupedT.First(gt2 => gt2.PlayerScore ==
groupedT.Max(gt => gt.PlayerScore)).PlayerName
};
FYI - I did run this code against your data and it worked (after I fixed that one, little data mistake).