title: Complex linq ranking with group sum and take
body: <p>I have sports application that scores competitors on their 3 best results. A tie breaker that is a count of the number of "first time" completions of their results. The code below takes their 3 best results, sums the value of those results, and counts the number of first time results. The output is grouped by gender and category (age group).</p>
  
  <pre><code>        public async Task&lt;ActionResult&gt; Prelims()
      {
          IQueryable&lt;CompResults&gt;
              compdata = from result in db.Result
                         where result.Complete == true
                         group result by new
                         {
                             result.CompetitorId,
                             result.Competitor.FirstName,
                             result.Competitor.LastName,
                             result.Competitor.Category,
                             result.Competitor.Gender,
                         } into resultsGroup
  
                         select new CompResults()
                         {
                             CompetitorId = resultsGroup.Key.CompetitorId,
                             Name = resultsGroup.Key.FirstName + " " + resultsGroup.Key.LastName,
                             Category = resultsGroup.Key.Category,
                             Gender = resultsGroup.Key.Gender,
                             PrelimsScore = resultsGroup.OrderByDescending(s =&gt; s.Event.Value).Take(3).Sum(s =&gt; s.Event.Value),
                             TotalFirstTime = resultsGroup.OrderByDescending(s =&gt; s.Event.Value).Take(3).Where(s =&gt; s.FirstTime == true).Count(),
                         };
  
          return View(await compdata.OrderBy(c =&gt; c.Category).ThenBy(c =&gt; c.Gender).ThenByDescending(c =&gt; c.PrelimsScore).ThenByDescending(c =&gt; c.TotalFirstTime).ToListAsync());
      }
  </code></pre>
  
  <p>I would like to add a ranking over category and gender. Can I modify my existing code or start with a new approach? I am hopelessly confused right now.</p>