如何从多个表中收集数据并创建类型化对象
How to collect data from multiple tables and create a typed object
我正在尝试从多个 table 中检索数据并为每个查询结果创建一个类型化对象 (Class)(我可以使用 Dapper参考)。
请在下面找到我的示例:
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; }
}
对于第一个查询,我需要获取 country table 的所有列和 只有一个 列来自 人table。
我应该获取 Person 对象 还是可以获取 typed 对象?
提前致谢!
您不应该 return Person
对象,因为 none 您的查询 return Person
。如果你想 return 一个有类型的对象,你需要定义一个类型;例如,对于您的第一个查询:
public class PersonCountryResponse
{
public string Person { get; init; }
public string Country { get; init; }
public string Capital { get; init; }
public decimal Area { get; init; }
public decimal Population { get; init; }
}
然后在你的第一个 select:
var pcrs = something.query(@"SELECT p.name AS personName, co.name AS countryName, 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")
.Select(q => new PersonCountryResponse { Person = q.personName, Country = q.countryName, Capital = q.capital, Area = q.area, Population = q.population };
我正在尝试从多个 table 中检索数据并为每个查询结果创建一个类型化对象 (Class)(我可以使用 Dapper参考)。
请在下面找到我的示例:
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; }
}
对于第一个查询,我需要获取 country table 的所有列和 只有一个 列来自 人table。 我应该获取 Person 对象 还是可以获取 typed 对象?
提前致谢!
您不应该 return Person
对象,因为 none 您的查询 return Person
。如果你想 return 一个有类型的对象,你需要定义一个类型;例如,对于您的第一个查询:
public class PersonCountryResponse
{
public string Person { get; init; }
public string Country { get; init; }
public string Capital { get; init; }
public decimal Area { get; init; }
public decimal Population { get; init; }
}
然后在你的第一个 select:
var pcrs = something.query(@"SELECT p.name AS personName, co.name AS countryName, 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")
.Select(q => new PersonCountryResponse { Person = q.personName, Country = q.countryName, Capital = q.capital, Area = q.area, Population = q.population };