1
Answer

Map lists of nested objects with Dapper (3 level nested obj)

Ameya Rane

Ameya Rane

7y
721
1
I'm using Dapper and I have classes like this:
 
 
  1. public class Region  
  2.  {  
  3.      public Region()  
  4.      {  
  5.          Countries = new List<Country>(0);  
  6.      }  
  7.      public int RegionID { getset; }  
  8.   
  9.      public int RiskRank { getset; }  
  10.   
  11.      public string Name { getset; }  
  12.   
  13.      public int SiteID { getset; }  
  14.   
  15.      public string DestinationType { getset; }  
  16.   
  17.      public string HealixCode { getset; }  
  18.   
  19.      public string AreaCode { getset; }  
  20.   
  21.      public List<Country> Countries { getset; }  
  22.  }  
  23.   
  24. public class Country  
  25.  {  
  26.      public Country()  
  27.      {  
  28.   
  29.      }  
  30.   
  31.      public int CountryID { getset; }  
  32.   
  33.      public bool IsSecondary { getset; }  
  34.   
  35.      public string Name { getset; }  
  36.   
  37.      public string ISO2Code { getset; }  
  38.   
  39.      public string ISO3Code { getset; }  
  40.   
  41.      public ISOCountry ISOCountry { getset; }  
  42.   
  43.      public IList<CountryAlias> CountryAliases { getset; }  
  44.  }  
  45.   
  46.   
  47.  public class CountryAlias  
  48.  {  
  49.      public CountryAlias()  
  50.      {  
  51.   
  52.      }  
  53.      public int CountryAliasID { getset; }  
  54.      public int CountryID { getset; }  
  55.      public string Alias { getset; }  
  56.  }   

I can get all information about Regions with all countries with but I would like to know if is possible with one query get also the list of CountryAlias for each country for each region. Actually what I do is this:

  1. private const string GetAllForSiteWithoutCountriesSQL = @"SELECT * FROM Regions WHERE ChannelID = @channelID";   
  2.   
  3. private const string GetAllForSiteWithCountriesSQL = @"SELECT c.*, rc.RegionID FROM Regions r JOIN RegionCountries rc ON rc.RegionID = r.RegionID JOIN Countries c ON (rc.CountryID = c.CountryID AND c.IsSecondary = 0) WHERE r.ChannelID = @channelID";  
  4.   
  5. public async Task<IEnumerable<Region>> GetAllAsync(int channelID, bool includeCountries = true)  
  6. {  
  7.    var regions = await Database.QueryAsync<Region> GetAllForSiteWithoutCountriesSQL, new { channelID });  
  8.   
  9.     var regionMap = regions.ToDictionary(r => r.RegionID);  
  10.   
  11.      if (includeCountries)  
  12.       {  
  13.              await Database.QueryAsync<Country, int, Country>(  
  14.                GetAllForSiteWithCountriesSQL,  
  15.                     (country, regionID) =>  
  16.                     {  
  17.                         regionMap[regionID].Countries.Add(country);  
  18.                         return country;  
  19.                     }, new { channelID }, splitOn: "RegionID");  
  20.        }  
  21.   
  22.        return regions;  
  23.  }  
 
I also found a good explanation here:-
 
https://stackoverflow.com/questions/7508322/how-do-i-map-lists-of-nested-objects-with-dapper/17748734#17748734
 
 but I don't understand how to use it in my case, because I have also the Group class. How should I do this with Dapper, is it possible or the only way is to do different steps? Thanks
 
 
 
Answers (1)