multi mapping dapper return one record code example
Example: reverse manytoone dapper
var lookup = new Dictionary<object, Users>();
var res = connection.QueryAsync<Users, Course, Users>(@"
SELECT *
FROM [Users] AS [u]
LEFT JOIN [Courses] AS [c] ON [u].[Id] = [c].[MasterId]
ORDER BY [u].[Id], [c].[Id]
", (user, course) =>
{
Users userObj;
if (!lookup.TryGetValue(user.Id, out userObj))
{
lookup.Add(user.Id, userObj = user);
}
if (userObj.MasterCourse == null)
{
userObj.MasterCourse = new List<Course>();
}
if (course != null)
{
userObj.MasterCourse.Add(course);
}
return userObj;
},
splitOn: "Id"
).Result;
return lookup.Values.ToList();