How can I create 1 or 2 objects per line with dapper?

I have a row with 4 columns of data. I want to create an object A from the data in columns 1-2. If there is no data in columns 1-2, use columns 3-4 to create object B. In rare cases, we will have data in all columns, but the data in columns 2 and 4 do not match. In this case, I want to return object A and object B.

Is there a way to do this in dapper using multi-mapping? Or should I return an object C that contains all 4 columns, and then send data processing to create the objects A and B that I really want?

public class A { public long ID {get;set;} public long Value {get;set;} } public class B { public long ID {get;set;} public long Value {get;set;} } 

Objects A and B are not related to each other (i.e. A does not contain a list B). Therefore, I am not sure how to proceed.

+4
source share
1 answer

If the data is partitioned on Id , then it should work if you use a shell type to represent two values. For example, if we use Tuple<,> , then:

 var data = conn.Query<A, B, Tuple<A, B>>(sql,(a, b) => Tuple.Create(a, b), args); 
+3
source

All Articles