Sql server连接/子选择问题

发布于 2024-10-21 14:22:18 字数 750 浏览 2 评论 0原文

我在数据库中有两个表

Team:Id(int PK), Name(nvarchar)

Player:Id(int PK), Name(nvarchar), Age(int), TeamId(int FK)

还有以下类

class Team
{
   public int Id{get;set;}
   public string Name{get;set;}
}

class Player
{
   public int Id{get;set;}
   public string Name{get;set;}
   public int Age{get;set;}
   public int TeamId{get;set;}
}

,并且

class TeamInfo
{
   public Team Team{get;set;}
   public List<Player> Players{get;set;}
}

想从数据库中提取 List。 我正在使用 Ado.net,没有 ef 或 linq2sql...我想问查询数据库的最佳方法是什么?

现在我得到了所有球队,对于每个球队,我通过连接查询表玩家,但可以肯定这不是最好的方法。

I have two tables in the database

Team:Id(int PK), Name(nvarchar)

and

Player:Id(int PK), Name(nvarchar), Age(int), TeamId(int FK)

I also have the following classes

class Team
{
   public int Id{get;set;}
   public string Name{get;set;}
}

and

class Player
{
   public int Id{get;set;}
   public string Name{get;set;}
   public int Age{get;set;}
   public int TeamId{get;set;}
}

and

class TeamInfo
{
   public Team Team{get;set;}
   public List<Player> Players{get;set;}
}

I want to extract from the databse a List<TeamInfo>.
I am using Ado.net, no ef or linq2sql... and I am asking what is the best way to query the db?

Right now I get all teams and for each team I query the table players with a join but for sure it's not the best way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-10-28 14:22:18

我会在sql server中使用参数@teamid创建一个sp,然后为团队执行sp并获取玩家信息

create procedure [dbo].[TeamPlayers]
as

@teamid int
begin

    set nocount on;

select
    t.ID as TeamID,
    t.name,
    p.ID as PlayerID,
    p.name,
    p.age
from dbo.Player p
    inner join Team t
        on p.TeamID=t.ID
where t.ID=@teamid


end

i'd create an sp in sql server with the parameters @teamid, then execute the sp for the team and get the player info

create procedure [dbo].[TeamPlayers]
as

@teamid int
begin

    set nocount on;

select
    t.ID as TeamID,
    t.name,
    p.ID as PlayerID,
    p.name,
    p.age
from dbo.Player p
    inner join Team t
        on p.TeamID=t.ID
where t.ID=@teamid


end
土豪 2024-10-28 14:22:18

您应该使用 join 查询从数据库读取所有信息:

SELECT 
       Team.Id as TeamId,
       Team.Name as TeamName,
       Player.Id as PlayerId,
       Player.Name as PlayerId,
FROM Team INNER JOIN Player on Team.Id = Player.TeamId

使用此查询作为 SqlCommand 的一部分,并将数据获取到 DataTable 中。然后:

var teamInfos = datatable.Rows
                    .Cast<DataRow>()
                    .GroupBy(dr => (int) dr["TeamId"])
                    .Select(t => new TeamInfo
                       {
                           Team = new Team{ Id = t.Key, 
                                            Name = (string)t.First["TeamName"] },
                           Players = t.Select(p => new PlayerInfo {
                                      Id = (int)p["PlayerId"],
                                      Name = (int)p["PlayerName"],
                                      // other player fields here
                                 })
                                 .ToList()
                       });

注意: 可能您需要在此处使用 LEFT 加入,而不是 INNER ,否则您将无法获得没有玩家的团队。

you should read all info from database with join query:

SELECT 
       Team.Id as TeamId,
       Team.Name as TeamName,
       Player.Id as PlayerId,
       Player.Name as PlayerId,
FROM Team INNER JOIN Player on Team.Id = Player.TeamId

Use this query as part of SqlCommand and get data into DataTable. Then:

var teamInfos = datatable.Rows
                    .Cast<DataRow>()
                    .GroupBy(dr => (int) dr["TeamId"])
                    .Select(t => new TeamInfo
                       {
                           Team = new Team{ Id = t.Key, 
                                            Name = (string)t.First["TeamName"] },
                           Players = t.Select(p => new PlayerInfo {
                                      Id = (int)p["PlayerId"],
                                      Name = (int)p["PlayerName"],
                                      // other player fields here
                                 })
                                 .ToList()
                       });

NOTE: Probably you need a LEFT join here instead of INNER since you won't get teams without players otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文