I have a table of employees that looks like the following tables. Every column is apart of a composite primary key and thus also a foreign key in the child tables:
I have created the following c# models to house this data:
public class Company
{
public string CompanyID {get; set;}
public List<Departments> Departments {get; set;}
}
public class Departments
{
public string CompanyID {get; set;}
public string DepartmentID {get; set;}
public List<Employee> Employees {get; set;}
}
public class Employees
{
public string CompanyID {get; set;}
public string DepartmentID {get; set;}
public string EmplyeeID {get; Set;}
}
I then have a repository in which I want to use dapper to populate the data in its entirety. Traditionally, in a non hierarchical implementation I would just do something such as the following:
public IEnumerable<Employee> GetEmployees()
{
IEnumerable<Employee> result = null;
string sql = "SELECT * FROM EMPLOYEE;"
using(IDbConnection c = Connection) //declared earlier with connection string
{
c.Open();
result = c.Query<Employee>(sql);
}
return result;
}
what makes me unsure is the most performant way to use dapper to build my desired data structure (at the company class level) such that the single company class contains a list of departments, departments have a list of employees.
I've tinkered with multiple queries and using several loops build the structure but it just seems so clunky and i cant quite get it right.
So here is the formal question:
How do we build nested enumerable objects to represent hierarchical data structures using dapper?



stringfor your ID values? They're clearly numeric. 2) Don't use ausingblock on a class-level object such asConnection. The containing class owns it, not the method, so you should implementIDisposableand dispose it accordingly. - madreflectionQueryMultiple, it would help to see what you did and maybe see why it was clunky. - madreflectionTupleto return all the objects in one database call - scgough