C # linq group by

How can I calculate, group and sort the following list based on money from people with linq?

        Person[] names = { new Person{ Name = "Harris", Money = 100 }, 
                                new Person{ Name = "David", Money = 100 },
                                new Person{Name = "Harris", Money = 150},
                                new Person{Name = "Mike", Money = 100},
                                new Person{Name = "Mike", Money = 30},
                                new Person{Name = "Mike", Money = 20} };

The result will return:

Harris 250 
Mike 150 
David 100
+3
source share
2 answers
from p in names
group p by p.Name into g
order by g.Key
select new { Name = g.Key, Amount = g.Sum(o => o.Amount) }
+8
source
 var personMoney = names.GroupBy(x=>x.Name)
                   .Select(x=>new {Name = x.Key, AllMoney = x.Sum(y=>y.Money)})
                   .OrderByDescending(x=>x.AllMoney).ToList();
+9
source

All Articles