簡體   English   中英

用dapper鏈接多個類

[英]Chaining multiple classes with 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參數的多圖查詢。 比較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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM