0

I have been going through Dapper's Multi Mapping documentation but remain confused.

This is the example from Dapper documentation

class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public User Owner { get; set; } } class User { public int Id { get; set; } public string Name { get; set; } } var sql = @"select * from #Posts p left join #Users u on u.Id = p.OwnerId Order by p.Id"; var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;}); var post = data.First(); 

What confuses me is where the variable post and user came from? I see in this line ...

connection.Query<Post, User, Post>

that Post and User are the models and I understand that ..

(post, user) => { post.Owner = user; return post;}

is a function that defines the mapping, but where do post and user get instantiated?

1
  • They don't "come from" anywhere, it's a lambda expression, go have a read about them :) Commented Apr 12, 2017 at 13:33

1 Answer 1

1

Its a Lambda function. Don't know the technical explanation, but basically connection.Query returns multiple records, each containing a post and user as per the query. With (post, user) => you're declaring that post will represent the Post and user will represent the User from each record.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.