C# 实体框架LINQ
我如何找到哪些俱乐部拥有最多的球员。因此,查询将包含所有拥有最多球员的俱乐部。例如,如果第一个俱乐部有3名球员,第二个俱乐部有5名球员,第三个俱乐部也有5名球员,那么查询中将有第二个和第三个俱乐部。我需要返回俱乐部的名单C# 实体框架LINQ,c#,entity-framework,linq,C#,Entity Framework,Linq,我如何找到哪些俱乐部拥有最多的球员。因此,查询将包含所有拥有最多球员的俱乐部。例如,如果第一个俱乐部有3名球员,第二个俱乐部有5名球员,第三个俱乐部也有5名球员,那么查询中将有第二个和第三个俱乐部。我需要返回俱乐部的名单 public List<Club> ClubWithMostPlayers() { using (var context = new dataContext()) { var query = ?????
public List<Club> ClubWithMostPlayers()
{
using (var context = new dataContext())
{
var query = ?????
return query.ToList();
}
}
公共列表ClubWithMostPlayers()
{
使用(var context=new dataContext())
{
变量查询=?????
返回query.ToList();
}
}
这些是我使用的类
public class Player
{
public int PlayerId { get; set; }
public string name { get; set; }
public string surname { get; set; }
public int yearOfBirth { get; set; }
public virtual List<PlayerClub> playerClub { get; set; }
}
public class Club
{
public int ClubId { get; set; }
public string name { get; set; }
public string stadium { get; set; }
public int yearOfConstruction { get; set; }
public virtual List<PlayerClub> playerClub { get; set; }
}
public class PlayerClub
{
public int PlayerClubId { get; set; }
public int PlayerId { get; set; }
public int ClubId { get; set; }
public virtual Player player { get; set; }
public virtual Club club { get; set; }
public int from { get; set; }
public int to { get; set; }
public int appearances { get; set; }
}
公共类播放器
{
public int PlayerId{get;set;}
公共字符串名称{get;set;}
公共字符串姓氏{get;set;}
公共整数出生年份{get;set;}
公共虚拟列表playerClub{get;set;}
}
公共班级俱乐部
{
public int ClubId{get;set;}
公共字符串名称{get;set;}
公共字符串体育场{get;set;}
公共构造年{get;set;}
公共虚拟列表playerClub{get;set;}
}
公营娱乐俱乐部
{
public int PlayerClubId{get;set;}
public int PlayerId{get;set;}
public int ClubId{get;set;}
公共虚拟播放器{get;set;}
公共虚拟俱乐部{get;set;}
来自{get;set;}的公共int
公共int到{get;set;}
公共int外观{get;set;}
}
我知道如何在SQL中实现这一点,但不知道如何在LINQ中实现这一点
谢谢您的帮助将查询分为两部分如何
// get number of maximum number of players in one club
var count = context.Clubs.Select(c => c.playerClub.Count()).Max();
// get clubs with specific number of players
var query = context.Clubs
.Where(c => c.playerClub.Count() == count);
谢谢,这是一个非常好的解决方案,代码不多:)