复杂的linq排名与组和总和

时间:2015-10-07 01:28:24

标签: c# linq asp.net-mvc-5 rank

我有运动应用程序,以3个最佳成绩为竞争对手打分。一个平局破坏者,是对其结果的“首次”完成次数的计数。下面的代码取3个最佳结果,将这些结果的值相加,并计算首次结果的数量。输出按性别和类别(年龄组)分组。

        public async Task<ActionResult> Prelims()
    {
        IQueryable<CompResults>
            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 => s.Event.Value).Take(3).Sum(s => s.Event.Value),
                           TotalFirstTime = resultsGroup.OrderByDescending(s => s.Event.Value).Take(3).Where(s => s.FirstTime == true).Count(),
                       };

        return View(await compdata.OrderBy(c => c.Category).ThenBy(c => c.Gender).ThenByDescending(c => c.PrelimsScore).ThenByDescending(c => c.TotalFirstTime).ToListAsync());
    }

我想在类别和性别上添加排名。我可以修改现有代码还是以新方法开始?我现在无可救药地混淆了。

0 个答案:

没有答案