Итак, я сделал что-то вроде следующего.
public static IEnumerable<Post> Map(this SqlMapper.GridReader reader, Func<Post, int> postKey, Func<Comment, int> commentKey, Func<Tag, int> TagKey, Action<Post, IEnumerable<Comment>> addPostComment, Action<Post, IEnumerable<Tag>> addPostTag)
{
var posts = reader.Read<Post>().ToList();
var comments = reader.Read<Comment>().GroupBy(t => commentKey(t)).ToDictionary(g => g.Key, g => g.AsEnumerable());
var tags = reader.Read<Tag>().GroupBy(t => TagKey(t)).ToDictionary(g => g.Key, g => g.AsEnumerable());
var authors = reader.Read<Author>().ToList();
foreach (var post in posts)
{
IEnumerable<Tag> posttags;
if (tags.TryGetValue(postKey(post), out posttags))
{
addPostTag(post, posttags);
}
IEnumerable<Comment> postcomments;
if (comments.TryGetValue(postKey(post), out postcomments))
{
addPostComment(post, postcomments);
}
post.Author = authors.Where(a => a.AuthorId == post.AuthorId).SingleOrDefault();
}
return posts;
}
Используя вышеуказанное расширение как показано ниже.
var query = "SELECT p.* FROM Post p " +
"SELECT c.* FROM Comment c WHERE c.PostId in ( SELECT PostId FROM Post) " +
"SELECT t.* FROM Tag t WHERE t.PostId in ( SELECT PostId FROM Post) " +
"SELECT a.* FROM Author a WHERE a.AuthorId in ( SELECT AuthorId FROM Post) ";
List<Post> result = new List<Post>();
using (var conn = new System.Data.SqlClient.SqlConnection(ConnectionString))
{
conn.Open();
try
{
var posts = conn.QueryMultiple(query)
.Map
(
p => p.PostId,
c => c.PostId,
t => t.PostId,
(p, c) => p.Comments.AddRange(c),
(p, t) => p.Tags.AddRange(t)
);
if (posts != null && posts.Any())
{
result.AddRange(posts);
}
}
catch (Exception)
{
//TODO: Log Exception
throw;
}
conn.Close();
}