C# 用dapper链接多个类

C# 用dapper链接多个类,c#,asp.net,dapper,C#,Asp.net,Dapper,例如,我有两个班 class User { string name {get;set;} int age {get;set;} Register reg {get;set;} } class Register { datetime time {get; set;} bool active {get;set;} } 我将查询设置为匹配属性,但我希望将值映射到类中的值 如何在dapper中实现这一点?您可以使用带有spliton参数的multimap查询。比较: 公共类帐户{

例如,我有两个班

class User
{
  string name {get;set;}
  int age {get;set;} 
  Register reg {get;set;}
}

class Register 
{
 datetime time {get; set;}
 bool active {get;set;}
}
我将查询设置为匹配属性,但我希望将值映射到类中的值


如何在dapper中实现这一点?

您可以使用带有spliton参数的multimap查询。比较:

公共类帐户{
公共int?Id{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字符串国家{get;set;}
public int ShopId{get;set;}
公共商店{get;set;}
}
公营商店{
公共int?ShopId{get;set;}
公共字符串名称{get;set;}
公共字符串Url{get;set;}
}
var resultList=conn.Query(@)
选择名称、地址、国家/地区、店铺ID
s、 ShopId、s.名称、s.Url
来自帐户a
s.ShopId=a.ShopId上的内部连接店铺s
“,(a,s)=>{
a、 Shop=s;
返回a;
},
斯普利顿:“ShopId”
).AsQueryable();
public class Account {
  public int? Id {get;set;}
  public string Name {get;set;}
  public string Address {get;set;}
  public string Country {get;set;}
  public int ShopId {get; set;}
  public Shop Shop {get;set;}
}
public class Shop {
  public int? ShopId {get;set;}
  public string Name {get;set;}
  public string Url {get;set;}
}

var resultList = conn.Query<Account, Shop, Account>(@"
                SELECT a.Name, a.Address, a.Country, a.ShopId
                        s.ShopId, s.Name, s.Url
                FROM Account a
                INNER JOIN Shop s ON s.ShopId = a.ShopId                    
                ", (a, s) => {
                     a.Shop = s;
                     return a;
                 },
                 splitOn: "ShopId"
                 ).AsQueryable();