/// <summary> /// /// </summary> /// <param name="level"></param> /// <param name="trimSuffix"></param> /// <returns></returns> public IEnumerable <AdministrativeDivisionCn> Get(Level level, bool trimSuffix = true) { if (level > Level.Village) { level = Level.County; } if (level > 0) { var data = dataSet.Where(_ => _.Level <= (int)level).ToList(); return(GetChildren(data, 0L, Level.Province).Select(province => new AdministrativeDivisionCn { Code = province.Code, Level = province.Level, Display = trimSuffix ? TrimProvinceSuffix(province.Display) : province.Display, Children = level > Level.Province ? GetChildren(data, province.Code, Level.City).Select(city => new AdministrativeDivisionCn { Code = city.Code, Level = city.Level, Display = trimSuffix ? TrimCitySuffix(city.Display) : city.Display, Children = level > Level.City ? GetChildren(data, city.Code, Level.County).Select(county => new AdministrativeDivisionCn { Code = county.Code, Level = county.Level, Display = trimSuffix ? TrimCountySuffix(county.Display) : county.Display, Children = level > Level.County ? GetChildren(data, county.Code, Level.Town).Select(town => new AdministrativeDivisionCn { Code = town.Code, Level = town.Level, Display = trimSuffix ? TrimTownSuffix(town.Display) : town.Display, Children = level > Level.Town ? GetChildren(data, town.Code, Level.Village).Select(village => new AdministrativeDivisionCn { Code = village.Code, Level = village.Level, Display = trimSuffix ? TrimVillageSuffix(village.Display) : village.Display, }) : null, }) : null, }) : null, }) : null, })); } else { return(new AdministrativeDivisionCn[0]); } }
/// <summary> /// /// </summary> /// <param name="code"></param> /// <param name="level"></param> /// <returns></returns> public IEnumerable <AdministrativeDivisionCn> GetChildren(long code, Level level) { return(GetChildren(dataSet, code, level)); }
static IEnumerable <AdministrativeDivisionCn> GetChildren(IEnumerable <AdministrativeDivisionCn> data, long code, Level level) { if (level == Level.Province) { return(data .Where(_ => _.Level == 1) .OrderBy(_ => _.Code) .ToList()); } var _base = level switch { Level.City => _province, Level.County => _city, Level.Town => _county, Level.Village => _town, _ => 1L, }; code = code / _base * _base; return(data .Where(_ => _.Level == (int)level && _.Code > code && _.Code < code + _base) .OrderBy(_ => _.Display) .ToList()); }