相关实体EF Count

时间:2018-03-05 07:52:44

标签: sql database entity-framework count foreign-keys

需要帮助。我想要来自俱乐部的数量球员,并显示有多少球员在俱乐部比赛。这是我的观看enter image description here

但我希望我的代码在俱乐部中显示球员数量,就像这样 enter image description here

这是我的数据库结构: enter image description here

我的开始代码

 public int GetCountOfPlayersInClub(int clubId)
    {
        using (var db = new BasketDbContext())
        {
            return db.Player.Count(p => p.BasketBallClubId == clubId);
        }
    }

但我现在做什么,我需要在ActionResult Index()中写什么?

1 个答案:

答案 0 :(得分:0)

要在视图中显示值,您需要将其传递给模型或视图包。 在视图中,您可以通过razor语法调用传递的变量(如@ Viebag.CountAll)。

请注意,ef核心中的count不是很优化,我建议为它运行原始查询。在测试中我做了,原始查询快了100倍。