Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从总LINQ查询中获取排名_Sql_Asp.net Mvc_Linq To Entities_Entity Framework 6 - Fatal编程技术网

Sql 从总LINQ查询中获取排名

Sql 从总LINQ查询中获取排名,sql,asp.net-mvc,linq-to-entities,entity-framework-6,Sql,Asp.net Mvc,Linq To Entities,Entity Framework 6,我有两张表,第一张是选手表和积分表。 具有ParticipateID的表之间存在外键关系。 在CompetitionPoints表中,多个participateID有不同的分数。因此,我想获取总分和基于总分的排名。因此,如果一个participateID有多个相同的总分,则该participateID的排名应该相同。它与学生总分和该分数的排名相同。 这是我的密码 var competitionusers = (from c in db.CompetitionUsers group c by ne

我有两张表,第一张是选手表和积分表。 具有ParticipateID的表之间存在外键关系。 在CompetitionPoints表中,多个participateID有不同的分数。因此,我想获取总分和基于总分的排名。因此,如果一个participateID有多个相同的总分,则该participateID的排名应该相同。它与学生总分和该分数的排名相同。 这是我的密码

var competitionusers = (from c in db.CompetitionUsers
group c by new { c.ParicipateId, c.CompetitionPoints.FirstOrDefault().Points }                                     
  into g orderby g.Key.Points descending select 
  new { Points = db.CompetitionPoints.Where
          (x => x.ParticiapteId == g.FirstOrDefault().ParicipateId).Sum(x => x.Points),   

Rank = (from o in db.CompetitionUsers 
   group o by o.CompetitionPoints.FirstOrDefault().Points into l                                                  
  select l).Count(s => s.Key > db.CompetitionPoints.
   Where(x => x.ParticiapteId == g.FirstOrDefault().ParicipateId).Sum(x => x.Points)) + 1,  
}).Where(x => x.Points != null).OrderByDescending(x => x.Points).AsQueryable();

如果我正确理解您的数据模型,我认为您可以简化为以下内容:

var result = db.CompetitionUsers
    // group by total points
    .GroupBy(cu => cu.CompetitionPoints.Sum(cp => cp.Points)) 
    // order by total points descending
    .OrderByDescending(g => g.Key) 
    // calculate rank based on position in grouped results
    .SelectMany((g, i) => g.Select(cu => new { Rank = i+1, TotalPoints = g.Key, CompetitionUser = cu })); 

您最好使用搜索引擎框架来实现类似的功能,例如,它内置了对文档评分的支持。你的意思是获得每个学生的总分,然后按顺序排列吗?我开始尝试格式化你的代码,以使其可读性更好,但我真的后悔了。@heymega是的,你是对的。我想要每个参与者的总分,然后按顺序排列。我已经获得了每个参与者的总分,但我无法使用LINQ@Jamiec我刚刚重新格式化了代码。你现在可以很容易地看到它向我展示了System.InvalidOperationException它对我不起作用。我通过使用新型号的iqueryable找到了解决方案。我首先得到了所有总分,然后我写了iqueryable查询,并获得了参与者的排名。谢谢你的帮助。
IQueryable<CompetitionLaderMadel> competitionUsers;
            competitionUsers = (from c in db.CompetitionUsers

select new CompetitionLaderMadel                                
{                                    
CompetitionName = c.Competition.CompetitionName,
                 CompetitionId = c.CompetitionId,                                    
                                    Points = db.CompetitionPoints.Where(x => x.ParticiapteId == c.ParicipateId).Sum(x => x.Points),
                                    IsFollow = db.CrowdMember.Any(x => x.Following == userid && x.UserCrowd.UserID == c.UserId && x.Status != Constants.Deleted),

}).Where(x => x.Points != null && x.UserId != null).OrderByDescending(x => x.Points);
var q = from s in competitionUsers
                    orderby s.Points descending
                    select new
                    {
                        CompetitionName = s.CompetitionName,
                        CompetitionId = s.CompetitionId,
                        HeadLine = s.HeadLine,
                        UserId = s.UserId,
                        Points = s.Points,
                        Image = s.Image,
                        IsFollow = s.IsFollow,
                        UserName = s.UserName,
                        Rank = (from o in competitionUsers
                                where o.Points > s.Points
                                select o).Count() + 1
                    };