c# - LINQ Lambda 连接与计数

标签 c# .net linq lambda

我想要获取所有没有当前团队的团队以及拥有 4 名或更多玩家的所有团队。 我尝试编写这个 linq lambda 查询:

teams = connection.Team 
    .Join(connection.Player,
        t => t.ID,
        p => p.IDTeam,
        (t, p) => new { Team = t, Player = p })
    .Where(tp => tp.Player.IDTeam == tp.Team.ID
        && tp.Team.ID != team.ID
        && tp.Team.IsVisible == true
        && !tp.Team.DeleteDate.HasValue)
    .Select(tp => tp.Team)
    .ToList();

但我无法计算在什么情况下有多少球员拥有球队。怎么做?哪个是 SQL 中的查询? 感谢您的帮助!

编辑: 根据需要,类(从 DBFirst 生成): No relation between the classes. The match is Team.ID == Player.IDTeam

最佳答案

基于上面的类,您可以扩展Team类并添加Players导航属性。

添加导航属性时,请确保TeamPlayer 表之间存在数据库关系。另外,如果需要,请配置您的 DbContext

public class Team
{
    //...other properties
    public virtual ICollection<Player> Players { get; set; }
}

当您添加导航属性时,实现您的要求将很简单:

connection.Teams
          .Where(t => t.ID != team.ID && t.IsVisible == true && !t.DeleteDate.HasValue && t.Players.Count() >= 4)

关于c# - LINQ Lambda 连接与计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50293415/

相关文章:

c# - ThreadState 已停止。没有异常(exception)。这是怎么回事?

c# - 本地 Postgres 的 Npgsql 连接字符串

c# - Linq C# 基于变量的查询

c# - 将展平表的 Linq 投影到父子对象图中

c# - 根据循环中另一个字典的值更新文本文件中的值

c#存储库模式 : One repository per subclass?

c# - 如何使用 Simple Injector 注册嵌套泛型类型?

c# - 在 C# 或 VB.NET 中不能做什么,您可以在 MSIL 中做什么?

c# - java 的 transient 关键字的 C# 等价物是什么?

c# - 按字段排序 (int)。如果字段不是int?