鉴于此测试:
public class Author
{
public int AuthorId { get; set; }
public List<Book> Books { get; set; } = new List<Book>();
}
public class Book
{
public int BookId { get; set; }
}
[Fact]
public async Task DapperCollapseLeftJoin()
{
var sql = @" select 1 as AuthorId, 1 as BookId
union select 1 as AuthorId, 2 as BookId
union select 2 as AuthorId, 3 as BookId
union select 2 as AuthorId, 4 as BookId";
var authorsWithBooks =
(await AC.OpenConnection.QueryAsync<Author, Book, Author>(sql,
(author, book) => { author.Books.Add(book); return author; },
splitOn: "AuthorId, BookId")).ToList();
// fails because we're getting 4 author+book rows
Assert.Equal(2, authorsWithBooks.Count);
}
Dapper的文档说多重映射将行映射到多个对象,并没有提及实际折叠对象的任何内容(就像最左边的列值重复的左/内连接一样)。
是否有一种Dapper-native方式来实现这一目标?