简体   繁体   English

实体框架LINQ

[英]Entity Framework LINQ

How do I find which clubs have the most players in query. 我该如何查询哪些俱乐部的球员最多。 So the query would contain all the clubs that have the most players. 因此,查询将包含所有拥有最多球员的俱乐部。 For example if first club has 3 players, second club has 5 players and third club also has 5 players then there would be second and third club in the query. 例如,如果第一家具乐部有3个玩家,第二家具乐部有5个玩家,而第三家具乐部也有5个玩家,则查询中将有第二和第三家具乐部。 And i need to return list of the clubs. 我需要返回俱乐部名单。

public List<Club> ClubWithMostPlayers()
{
     using (var context = new dataContext())
     {
          var query = ?????

          return query.ToList();
     }
}

These are classes that i use 这些是我使用的类

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; }
}

I know how to do it in SQL, but not in LINQ 我知道如何用SQL而不是LINQ做到这一点

Thank you for help 谢谢你的帮助

How about dividing your query into two parts. 如何将查询分为两部分。

// 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM