Dapper multimapping和left join

时间:2017-05-09 13:37:26

标签: dapper

鉴于此测试:

    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方式来实现这一目标?

1 个答案:

答案 0 :(得分:1)

目前没有内置机制来进行此展平,但测试套件here中有一个示例。它并不是非常漂亮,这是我喜欢以获得更好的支持,但它从来没有达到要做的事情列表的顶部。