How to split a SQL Select query into two Model C# with Dapper ORM

半腔热情 提交于 2020-02-04 05:48:13

问题


I want to split data into two separate models with DAPPER ORM in C#, From TeamId to Location between a column in one model and RowNumber to PageCount into another sperate model.


回答1:


I believe what you are trying to simulate would be an object such as:

public class Team
{
     public int Id { get; set; }
     public string Name { get; set; }
     public Supervisor Supervisor { get; set; }
     public Location Location { get; set; }
}

You can actually achieve this with Dapper up to seven objects, basically what you would do in your SQL would be:

public IEnumerable<Team> GetTeams() => dbConnection.Query<Team, Supervisor, Location, Team>(query, 
(team, supervisor, location, team) => 
{
   team.Supervisor = supervisor,
   team.Location = location,
   return team;  
});

You can find documentation on their multi object mapping here. I should denote it splits on Id unless specified and the order the objects appear in query result matter.



来源:https://stackoverflow.com/questions/58870494/how-to-split-a-sql-select-query-into-two-model-c-sharp-with-dapper-orm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!