Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple inner join result with Dapper?

Tags:

dapper

I can't seem to find documentation or examples for my problem (been searching a while now). I think my problem is pretty straightforward, so here goes.

I have two tables. My primary table is called Persons and the secondary table is PersonEntries. For each person in Person table, i can have 0 or more entries in the PersonEntries table. Like this.

Table: Person Id Name  Table: PersonEntry PersonId CheckinTime CheckoutTime 

I have two objects like this

public class Person {   public string Name;   public List<PersonEntry> PersonEntries; }  public class PersonEntry {   public DateTime CheckinTime;   public DateTime CheckoutTime; } 

If i was to get it from the database into my c# classes how would i do it? I can map a single table into my c# class and do it for each table, but then i'm left to match what entries maps to what person.

I've seen several examples of mapping ONE PersonEntry to ONE Person, the problem here is that i have a zero-to-many relation. My Person have a LIST of PersonEntry items.

like image 880
Per Hornshøj-Schierbeck Avatar asked Dec 10 '13 10:12

Per Hornshøj-Schierbeck


People also ask

What is inner join with example?

Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same for both the students and courses tables.

What is splitOn in dapper?

splitOn: CustomerId will result in a null customer name. If you specify CustomerId,CustomerName as split points, dapper assumes you are trying to split up the result set into 3 objects. First starts at the beginning, second starts at CustomerId , third at CustomerName .

Can I use Linq with dapper?

Dapper Plus LINQ DynamicYou can execute query dynamically through the Eval-Expression.NET library. The Eval-Expression.NET library can be activated with the Dapper Plus license.


1 Answers

You can do something like this (see https://www.tritac.com/blog/dappernet-by-example):

public class Shop {   public int? Id {get;set;}   public string Name {get;set;}   public string Url {get;set;}   public IList<Account> Accounts {get;set;} }  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;} }  var lookup = new Dictionary<int, Shop>() conn.Query<Shop, Account, Shop>(@"                 SELECT s.*, a.*                 FROM Shop s                 INNER JOIN Account a ON s.ShopId = a.ShopId                                     ", (s, a) => {                      Shop shop;                      if (!lookup.TryGetValue(s.Id, out shop)) {                          lookup.Add(s.Id, shop = s);                      }                      if (shop.Accounts == null)                           shop.Accounts = new List<Account>();                      shop.Accounts.Add(a);                      return shop;                  }                  ).AsQueryable(); var resultList = lookup.Values; 
like image 184
Jeroen K Avatar answered Sep 20 '22 21:09

Jeroen K