James Andrew Smith
James Andrew Smith

Reputation: 1566

Chaining multiple classes with dapper

I have two class for example

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

class Register 
{
 datetime time {get; set;}
 bool active {get;set;}
}

I have the query set up to match the properties but I want to map the values to the values in my classses.

How would I get this to work in dapper?

Upvotes: 1

Views: 1526

Answers (1)

Jeroen K
Jeroen K

Reputation: 10438

You can use a multimap query, with the spliton argument. Compare 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();

Upvotes: 1

Related Questions