Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying into a complex object with sqlkata

Tags:

dapper

sqlkata

I cant built a complex object with a query. How I do?

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Contact Contact { get; set; }
}

public class Contact
{
    public long Id { get; set; }
    public string FoneNumber { get; set; }
}
like image 502
VFB Avatar asked Oct 19 '25 09:10

VFB


1 Answers

You can use a combination of SqlKata (@amd's answer) and Dapper (@Void Ray's answer):

var query = new SqlKata.Query(...);      //compose your sqlkata query as user amd indicated above
var compiler = new PostgresCompiler();   //or mssqlcompiler or whatever
var compiled = compiler.Compile(query);
var sql = compiled.Sql;
var parameters = new DynamicParameters(compiled.NamedBindings);
var result = await db.QueryAsync<Person, Contact,Person>(sql,(p,c)=>{p.Contact = c;  return p;},parameters);
like image 95
BlackjacketMack Avatar answered Oct 22 '25 03:10

BlackjacketMack