Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ Query - Group By

Tags:

c#

.net

linq

Order the result and return the first one.

var callStats = (from c in database.CallLogs
                 group c by c.RemoteParty into d
                 select new
                 {
                      RemoteParty = d.Key,
                      TotalDuration = d.Sum(x => x.Duration)
                 });

callStats = callStats.OrderByDescending( a => a.TotalDuration )
                     .FirstOrDefault();

Have a look at the "Max" extension method from linq

callStats.Max(g=>g.TotalDuration);