简体   繁体   English

Dapper SQL 将表连接到另一个

[英]Dapper SQL Join table into another

Newbie here.新手来了I have the following 2 tables.我有以下 2 张桌子。 One for Albums:一张专辑:

在此处输入图像描述

And the other for Songs:另一个是歌曲:

在此处输入图像描述

So ALL of these songs belong to the "Freedom" album.所以所有这些歌曲都属于“自由”专辑。 In an api call I'm using dapper to display all the albums which is fine.在 api 通话中,我正在使用 dapper 显示所有专辑,这很好。

[HttpGet("[action]/{id}")]
        public async Task<IActionResult> GetSingleAlbumViaDapper(int id)
        {
            var sql = "SELECT * FROM Albums WHERE Id = @AlbumId";
            var album = (await _dbDapper.QueryAsync<Album>(sql, new { AlbumId = id })).SingleOrDefault();
        }

And here's the result:结果如下:

在此处输入图像描述

But I want to join the Songs relevant for this album.但我想加入与这张专辑相关的歌曲。 I've tried the following but it's not working, any ideas why?我已经尝试了以下但它不起作用,任何想法为什么?

[HttpGet("[action]/{id}")]
        public async Task<IActionResult> GetSingleAlbumViaDapper(int id)
        {
            var sql = "SELECT * FROM Albums WHERE Id = @AlbumId JOIN Songs in Songs on Albums.Songs";
            var album = (await _dbDapper.QueryAsync<Album>(sql, new { AlbumId = id })).SingleOrDefault();
        }

All it says is: Incorrect syntax near the keyword 'JOIN' .它只是说: Incorrect syntax near the keyword 'JOIN'

There is an error in how you're joining tables.您连接表格的方式有误。 First join, then filter (where-part)先加入,再过滤(where-part)

The correct query would be something like that正确的查询是这样的

SELECT * 
  FROM Albums a
  JOIN Songs s 
    ON s.AlbumId = a.Id
 WHERE a.Id = @AlbumId;

UPD. UPD。 Since you're looking for songs only in the second query, from your table scema I see join is not needed here.由于您仅在第二个查询中查找歌曲,因此从您的表 scema 中我看到此处不需要 join。 You're already passing album.id to the query.您已经将 album.id 传递给查询。 Try this one and let me know the result试试这个,让我知道结果

SELECT * 
  FROM Songs s 
 WHERE s.AlbumId = @AlbumId;

Try to change your join query as follows:尝试按如下方式更改您的联接查询:

SELECT a.* FROM Albums as a inner join Songs as s on s.AlbumId = a.Id WHERE a.Id = @AlbumId

You need to use the join syntax for Dapper.您需要使用 Dapper 的连接语法。 I assume you have a Song class, I also assume your Album constructor creates the Songs list.我假设您有一首歌曲 class,我还假设您的专辑构造函数创建了歌曲列表。 Then you would do it as below, where you tell dapper to expect Albums and Songs and return an Album.然后你会像下面那样做,你告诉 dapper 期待专辑和歌曲并返回专辑。 The data should be split on the Id from Songs, that's what the splitOn parameter is for.数据应该在歌曲的 ID 上拆分,这就是 splitOn 参数的用途。 This is typed from memory and with missing information, so it might require some adapting.这是从 memory 输入的,并且缺少信息,因此可能需要进行一些调整。

var sql = "SELECT * FROM Albums AS a INNER JOIN Songs AS s ON s.AlbumId = a.Id WHERE a.Id = @AlbumId";
Album foundAlbum = null;
var album = (await _dbDapper.QueryAsync<Album, Song, Album>(sql, (album, song) => 
    {
        if (foundAlbum is null)
        {
            foundAlbum = album;
        }
        foundAlbum.Songs.Add(song);
        return album;
    }, splitOn : "Id", new { AlbumId = id })).SingleOrDefault();
// Use foundAlbum here....

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

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