C# Linq根据外键在另一个列表中的出现情况查询订单列表

C# Linq根据外键在另一个列表中的出现情况查询订单列表,c#,asp.net,linq,C#,Asp.net,Linq,我有两门课,简介和下载。Download具有外键ProfileID,它映射到配置文件中的ID。下载中的每一行代表连接的配置文件的一次下载 我在进行linq查询时遇到问题,该查询获取按下载次数排序的配置文件列表 编辑: 这是我到目前为止在函数中得到的结果 IndexViewModel model = new IndexViewModel(); model.NewSubtitles = (from Profile in _ProfileRepo.GetAll()

我有两门课,简介和下载。Download具有外键ProfileID,它映射到配置文件中的ID。下载中的每一行代表连接的配置文件的一次下载

我在进行linq查询时遇到问题,该查询获取按下载次数排序的配置文件列表

编辑: 这是我到目前为止在函数中得到的结果

    IndexViewModel model = new IndexViewModel();
    model.NewSubtitles = (from Profile in _ProfileRepo.GetAll()
                      orderby Profile.ID descending
                      select Profile).Take(5).ToList();

    // This doesn't work:
    // model.TopSubtitles = (from n in _ProfileRepo.GetAll()
    //                       join d in _DownloadRepo.GetAll() on n.ID equals d.ProfileID into c
    //                       group c by c.ProfileID into g
    //                       orderby g.Count() descending
    //                       select n).Take(5).ToList();

        return View(model);

你有没有试过这样的方法:

from d in Downloads
orderby d.Profiles.Count()
...
试试这个:

     model.NewSubtitles = (from Profile in _ProfileRepo.GetAll()
                  join downloads in _DownloadRepo.GetAll() on Profile.UserId equals downloads.UserId
                 group downloads by Profile into p
                  orderby p.Count() descending
                  select new {p.Key.UserId , p.Key.UserName , p.Count()).Take(5).ToList();

你应该做你想做的事:

model.TopSubtitles =  (from p in _ProfileRepo.GetAll()
                      join d in _DownloadRepo.GetAll() on p.ID equals d.ProfileId
                      group d by p into g
                      orderby g.Count() descending
                      select g.Key).Take(5).ToList();
对于LINQ语法的挑战:

model.TopSubtitles = _ProfileRepo.GetAll()
  .Join(_DownloadRepo.GetAll(), p => p.ID, d => d.ProfileId, (p, d) => new { Profile = p, Download = d })
  .GroupBy(x => x.Profile)
  .OrderByDescending(g => g.Count())
  .Select (g => g.Key)
  .Take(5)
  .ToList();

你有没有已经准备好的代码让我们看一下?我把它添加到了原来的帖子里。