Dapper嵌套列表查询

时间:2018-04-29 22:35:40

标签: list nested dapper

我有3个POCO课程如下;

Continent有很多ContinentPart

大陆有很多国家

国家有很多城市

我想通过ContinentParts,国家和城市获得Continens

using (IDbConnection db = new SqlConnection(_conf["ConnectionStrings:WorkConStr"]))
            {
                string query = @"SELECT * FROM Continent as c
                                LEFT JOIN ContinentPart as cp ON c.ContinentId=cp.ContinentId
                                LEFT JOIN Country as co ON c.ContinentId=co.ContinentId
                                LEFT JOIN City ci ON co.CountryId=ci.CountryId
                                ORDER BY c.RecDate DESC";

                var continentDictionary = new Dictionary<int, Continent>();
                var list = db.Query<Continent, ContinentPart, Country, City, Continent>(
                    query,
                    map: (continent, continentPart, country, city) =>
                     {
                         Continent m;
                         if (!continentDictionary.TryGetValue(continent.ContinentId, out m))
                         {
                             continentDictionary.Add(continent.ContinentId, m = continent);
                         }
                         if (m.ContinentParts == null)
                             m.ContinentParts = new List<ContinentPart>();
                         m.ContinentParts.Add(continentPart);
                         if (m.Countries == null)
                             m.Countries = new List<Country>();
                         m.Countries.Add(country);
                         foreach (var h in m.Countries)
                         {
                             if (h.Cities == null)
                                 h.Cities = new List<City>();
                             h.Cities.Add(city);
                         }
                         return m;
                     },
                    param: new { },
                    splitOn: "")
                    .Distinct()
                    .ToList();
                return list;
            }

1 个答案:

答案 0 :(得分:0)

这里有两种选择。您可以使用多个结果集功能:

https://medium.com/dapper-net/handling-multiple-resultsets-4b108a8c5172

或者,在我看来,另一个选项是将联接结果作为JSON返回,例如使用如下查询:

select 
    continents.continent_id,
    continents.continent,
    countries.country_id,
    countries.country,
    cities.city_id,
    cities.city 
from 
    dbo.Continent continents
inner join 
    dbo.Country countries on continents.continent_id = countries.continent_id
inner join
    dbo.City cities on countries.country_id = cities.country_id
for 
    json auto

返回如下的JSON:

{
    "continent_id": 1,
    "continent": "Africa",
    "countries":
    [
        {
            "country_id": 1,
            "country": "Nigeria",
            "cities":           
            [   
                {
                    "city_id": 1,
                    "city": "Lagos"
                }, {
                    "city_id": 2,
                    "city": "Abuja"
                }
            ]
        }
    ]
}

然后可以使用自定义类型处理将其转换为复杂对象:

https://medium.com/dapper-net/custom-type-handling-4b447b97c620

链接是我在这个主题上撰写的文章。