multiple relationship using dapper code example
Example: multiple relationship using dapper
using (var connection = new SQLiteConnection(connString)){ var sql = @"select p.postid, headline, firstname, lastname, t.tagid, tagname from posts p inner join authors a on p.authorid = a.authorid inner join posttags pt on pt.postid = p.postid inner join tags t on t.tagid = pt.tagid"; var posts = await connection.QueryAsync<Post, Author, Tag, Post>(sql, (post, author, tag) => { post.Author = author; post.Tags.Add(tag); return post; }, splitOn: "firstname, tagid"); var result = posts.GroupBy(p => p.PostId).Select(g => { var groupedPost = g.First(); groupedPost.Tags = g.Select(p => p.Tags.Single()).ToList(); return groupedPost; }); foreach(var post in result) { Console.Write($"{post.Headline}: "); foreach(var tag in post.Tags) { Console.Write($" {tag.TagName} "); } Console.Write($" by {post.Author.FirstName} {post.Author.LastName} {Environment.NewLine}"); }}