相关实体EF Count

问题描述 投票:0回答:1

需要帮忙。我想要来自俱乐部的数量球员,并显示有多少球员在俱乐部比赛。这是我的观点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()中写什么?

sql database entity-framework count foreign-keys
1个回答
0
投票

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

请注意,计数在ef核心中没有得到很好的优化,我建议为它运行原始查询。在测试中我做了,原始查询快了100倍。

© www.soinside.com 2019 - 2024. All rights reserved.