Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6204099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:01:55+00:00 2026-05-24T05:01:55+00:00

I have following classes and db schema. I am trying to query this data

  • 0

I have following classes and db schema. I am trying to query this data from database using dapper that would hydrate the full object graph. I looked at various SO question and the test but couldn’t really figure out how to do this.

DB Schema

Author
  -AuthorId
  -Name

Post
  -PostId
  -Content
  -AuthorId

Comment
  -PostId
  -CommentId
  -Content

Tag
  -PostId
  -TagId
  -Name

Classes

public class Author
{
    public int AuthorId { get; set; }
    public string Name { get; set; }
}

public class Tag
{
    public int PostId { get; set; }
    public int TagId { get; set; }
    public string Name { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Content { get; set; }
    public int AuthorId { get; set; }
    public List<Tag> Tags { get; set; }
    public List<Comment> Comments { get; set; }
    public Author Author { get; set; }

    public Post()
    {
        this.Comments = new List<Comment>();
        this.Tags = new List<Tag>();
    }
}

public class Comment
{
    public int PostId { get; set; }
    public int CommentId { get; set; }
    public string Content { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:01:56+00:00Added an answer on May 24, 2026 at 5:01 am

    So I ended up doing something like the following.

    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;
    }
    

    Using the above extension as below.

            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();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-many relationship which I am trying to query. My problem is
I am trying to use Generic DataContract class so that I don't have to
I am trying to generate ServiceClient classes. I have 2 Projects, API and WEB.
The scenario is the following: I have a java package ( myapp.config ) that
I have following Classes and interfaces.. public interface ITaggable { ICollection<Tag> Tags { get;
I have a table that stores customer items. The table needs to reflect the
I am using Ruby on Rails 3 and I would like to set a
Okay, so i've studied c# and asp.net long enough and would like to know
I'm working with a node/link structure, but I'm having problems mapping it using fluent
I am new to Hibernate. I have Order and Account table in DB, CREATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.