用dapper链接多个类

时间:2013-10-03 16:02:21

标签: 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;}
}

我设置的查询与属性匹配,但我想将值映射到我的类中的值。

我如何才能在短小精悍的工作中使用它?

1 个答案:

答案 0 :(得分:0)

您可以使用带有spliton参数的多图查询。比较http://www.tritac.com/bp-24-dapper-net-by-example

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();