Answers for "reverse manytoone dapper"

0

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();
Posted by: Guest on January-01-2021

Browse Popular Code Answers by Language