text: So far, we have performed operations on a single row. But what if we need to deal with more complex objects that have parent-child relationships. Let's consider our example. We have a one-to-many relationship between an
  Author and
  Book. A single author can have many books.
  If you look at the
  Author, you can see a list of books.
  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DapperDemo { class Author { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List<Book> Books { get; set; } } }
  In a database, you can see on the first row of
  Authors that Cardinal Tom B. Erichsen has an
  Id = 1.
  If you look in the
  Books table, you can see that there are two different rows that have an
  AuthorId of 1. So Cardinal Tom B. Erichsen has written multiple books.
  So let's retrieve the authors from a database and their respective books using the
  QueryMultiple extension method. It can execute multiple queries within the same command and map results.
  private static void GetAuthorAndTheirBooks(int id) { string sql = "SELECT * FROM Authors WHERE Id = @Id;" + "SELECT * FROM Books WHERE AuthorId = @Id;"; using (IDbConnection db = new SqlConnection(ConnectionString)) { using (var results = db.QueryMultiple(sql, new { Id = id})) { var author = results.Read<Author>().SingleOrDefault(); var books = results.Read<Book>().ToList(); if (author != null && books != null) { author.Books = books; Console.WriteLine(author.FirstName + " " + author.LastName); foreach (var book in author.Books) { Console.WriteLine("\t Title: {0} \t Category: {1}", book.Title, book.Category); } } } } }) { GetAuthorAndTheirBooks(1); GetAuthorAndTheirBooks(2); }