title: C# LINQ Query - Group By
body: <p>I'm having a hard time understanding how I can form a LINQ query to do the following:</p>
  <p>I have a table CallLogs and I want to get back a single result which represents the call that has the longest duration.</p>
  <p>The row looks like this:</p>
  <blockquote>
  <p>[ID] [RemoteParty] [Duration]</p>
  </blockquote>
  <p>There can be multiple rows for the same RemoteParty, each which represents a call of a particular duration. I'm wanting to know which RemoteParty has the longest total duration.</p>
  <p>Using LINQ, I got this far:</p>
  <pre><code>var callStats = (from c in database.CallLogs
                   group c by c.RemoteParty into d
                   select new
                   {
                        RemoteParty = d.Key,
                        TotalDuration = d.Sum(x =&gt; x.Duration)
                   });
  </code></pre>
  <p>So now I have a grouped result with the total duration for each RemoteParty but I need the maximum single result.</p>
  <blockquote>
  <p>[DistinctRemoteParty1] [Duration]</p>
  <p>[DistinctRemoteParty2] [Duration]</p>
  <p>[DistinctRemotePartyN] [Duration]</p>
  </blockquote>
  <p>How can I modify the query to achieve this?</p>