'How to collect data from multiple tables and create a typed object
I'm trying to retrieve data from multiple tables and create a typed objects (Class) for each query result (I can use Dapper reference).
Please find below my example:
List<ClassA> country = query<ClassA>(@"SELECT p.name, co.name, co.capital, co.area, co.population
FROM t_person p
INNER JOIN t_city c ON p.city_id = c.city_id
INNER JOIN t_country co ON c.country_id = co.country_id");
List<ClassB> city = query<ClassB>(@"SELECT p.name, c.name
FROM t_person p
INNER JOIN t_city c ON p.city_id = c.city_i");
public class Person
{
public int PersonId { get; set; }
public string PersonName { get; set; }
public City City { get; set; }
}
public class City
{
public int CityId { get; set; }
public string CityName { get; set; }
public Country Country { get; set; }
}
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public string CountryCapital { get; set; }
public decimal CountryArea { get; set; }
public decimal CountryPopulation { get; set; }
}
For the first query, I need get all columns of country table and only one column from person table. Should I get Person object or can I get a typed object?
Thank you in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|