I've got three classes User, Order & Project which are stored in single tables. The orders and projects both have a n:n relation with the users. To implement that I've got two crosstables (UserOrders, UserProjects) which map these relations.
public class User { public string UserID {get;set;} public List<string> Orders{get;set;} public List<string> Projects {get;set;} } public class Order { public string OrderID {get;set} ... } public class Project { public string ProjectID {get;set} ... } As you can see the User object contains a list of every related orderID/projectID.
Now I want to query this with Dapper. I' ve got this solution which works pretty fine with one list. But if I try to query the complete user object for the 2nd list I'll get every result multiplied with the number of results in the first list. So if a user got 3 orders and 2 projects the orderlist will be fine and the projectlist will contain both projects 3 times:
var lookup = new Dictionary<string, User>(); var multi = dbDapperFM.Query<User, string, string, User>("SELECT u.*, uo.OrderID, up.ProjectID "+ "FROM User u INNER JOIN UserOrders uo ON u.UserID=uo.UserID "+ "INNER JOIN UserProjects up ON u.UserID=up.UserID", (u, uo, up) => { User user; if (!lookup.TryGetValue(m.UserID, out user)) lookup.Add(u.UserID, user= u); if (user.Orders == null) user.Orders = new List<string>(); user.Orders.Add(uo); if (user.Projects == null) user.Projects = new List<string>(); user.Projects.Add(up); return user; }, splitOn: "UserID , OrderID, ProjectID ").AsQueryable(); I understand why this problem occures (2 inner joins), but I don't really get how to solve it.