C# 递归省市区三级树结构
省市区结构
实体
/// <summary> /// 行政区 /// </summary> public class AreaEntity { public int Id { get; set; } /// <summary> /// 名称 /// </summary> public string RegionName { get; set; } /// <summary> /// 编码 /// </summary> public string RegionCode { get; set; } /// <summary> /// 上级编码 /// </summary> public string ParentCode { get; set; } public string Longitude { get; set; } public string Latitude { get; set; } } public class AreaModel { /// <summary> /// 名称 /// </summary> public string RegionName { get; set; } /// <summary> /// 编码 /// </summary> public string RegionCode { get; set; } /// <summary> /// 上级编码 /// </summary> public string ParentCode { get; set; } public AreaModel[] Children { get; set; } }
C# 代码
/// <summary> /// 获取所属获取信息 /// </summary> /// <param name="parentCode">所属编码</param> /// <returns>列表</returns> public AreaEntity[] GetAreaModelList(string parentCode) { StringBuilder strSQL = new StringBuilder(); strSQL.Append(" select * from [dbo].[Area] With(nolock) "); strSQL.Append(" WHERE ParentCode=@ParentCode"); var param = new SqlParameter[] { new SqlParameter("@ParentCode",SqlDbType.VarChar) }; param[0].Value = parentCode; AreaEntity[] list = null; using (var context = new YTODBDbContext()) { list = context.Database.SqlQuery<AreaEntity>(strSQL.ToString(), param).ToArray(); } return list; } /// <summary> /// 生成树的方法 /// </summary> /// <param name="parentCode">所属编码</param> /// <param name="areaModel">信息</param> /// <returns></returns> public AreaModel creatTheTree(string parentCode, AreaModel areaModel) { AreaEntity[] items = GetAreaModelList(parentCode); //如果没有字节点了,那就返回空 if (items.Length == 0) return null; List<AreaModel> jdList = new List<AreaModel>(); for (int i = 0; i < items.Length; i++) { AreaModel jiedian = new AreaModel(); jiedian.RegionName = items[i].RegionName; jiedian.RegionCode = items[i].RegionCode; jiedian.ParentCode = items[i].ParentCode; //递归循环 creatTheTree(items[i].RegionCode.ToString(), jiedian); jdList.Add(jiedian); } areaModel.Children = jdList.ToArray(); return areaModel; }
接口调用
public HttpResponseMessage getAreaInfo(AreaInfoViewModel req) { //var userinfo = GetCurrentLoginInfo(); //if (userinfo.RoleId != FlagMgr.UnitRole.Type.Agricultural.ToInt()) //{ // return ResponseError("权限不匹配!"); //} var AreaModel = _areaService.GetAreaModelList(req.RegionCode); var tempAreaModel = new AreaModel(); tempAreaModel.RegionName = AreaModel[0].RegionName; tempAreaModel.RegionCode = AreaModel[0].RegionCode; tempAreaModel.ParentCode = AreaModel[0].ParentCode; //递归树结构 var areaModel = _areaService.creatTheTree(req.RegionCode, tempAreaModel); return ResponseOK<AreaModel>(areaModel); }
返回数据
{ "mStatusCode": 200, "mStatusDesc": "OK", "mDataBody": { "RegionName": "济南市", "RegionCode": "370100", "ParentCode": "370000", "Children": [ { "RegionName": "济南市", "RegionCode": "370100", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370101", "ParentCode": "370100", "Children": null }, { "RegionName": "历下区", "RegionCode": "370102", "ParentCode": "370100", "Children": null }, { "RegionName": "市中区", "RegionCode": "370103", "ParentCode": "370100", "Children": null }, { "RegionName": "槐荫区", "RegionCode": "370104", "ParentCode": "370100", "Children": null }, { "RegionName": "天桥区", "RegionCode": "370105", "ParentCode": "370100", "Children": null }, { "RegionName": "历城区", "RegionCode": "370112", "ParentCode": "370100", "Children": null }, { "RegionName": "长清区", "RegionCode": "370113", "ParentCode": "370100", "Children": null }, { "RegionName": "平阴县", "RegionCode": "370124", "ParentCode": "370100", "Children": null }, { "RegionName": "济阳县", "RegionCode": "370125", "ParentCode": "370100", "Children": null }, { "RegionName": "商河县", "RegionCode": "370126", "ParentCode": "370100", "Children": null }, { "RegionName": "章丘市", "RegionCode": "370181", "ParentCode": "370100", "Children": null } ] }, { "RegionName": "青岛市", "RegionCode": "370200", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370201", "ParentCode": "370200", "Children": null }, { "RegionName": "市南区", "RegionCode": "370202", "ParentCode": "370200", "Children": null }, { "RegionName": "市北区", "RegionCode": "370203", "ParentCode": "370200", "Children": null }, { "RegionName": "黄岛区", "RegionCode": "370211", "ParentCode": "370200", "Children": null }, { "RegionName": "崂山区", "RegionCode": "370212", "ParentCode": "370200", "Children": null }, { "RegionName": "李沧区", "RegionCode": "370213", "ParentCode": "370200", "Children": null }, { "RegionName": "城阳区", "RegionCode": "370214", "ParentCode": "370200", "Children": null }, { "RegionName": "胶州市", "RegionCode": "370281", "ParentCode": "370200", "Children": null }, { "RegionName": "即墨市", "RegionCode": "370282", "ParentCode": "370200", "Children": null }, { "RegionName": "平度市", "RegionCode": "370283", "ParentCode": "370200", "Children": null }, { "RegionName": "莱西市", "RegionCode": "370285", "ParentCode": "370200", "Children": null } ] }, { "RegionName": "淄博市", "RegionCode": "370300", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370301", "ParentCode": "370300", "Children": null }, { "RegionName": "淄川区", "RegionCode": "370302", "ParentCode": "370300", "Children": null }, { "RegionName": "张店区", "RegionCode": "370303", "ParentCode": "370300", "Children": null }, { "RegionName": "博山区", "RegionCode": "370304", "ParentCode": "370300", "Children": null }, { "RegionName": "临淄区", "RegionCode": "370305", "ParentCode": "370300", "Children": null }, { "RegionName": "周村区", "RegionCode": "370306", "ParentCode": "370300", "Children": null }, { "RegionName": "桓台县", "RegionCode": "370321", "ParentCode": "370300", "Children": null }, { "RegionName": "高青县", "RegionCode": "370322", "ParentCode": "370300", "Children": null }, { "RegionName": "沂源县", "RegionCode": "370323", "ParentCode": "370300", "Children": null } ] }, { "RegionName": "枣庄市", "RegionCode": "370400", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370401", "ParentCode": "370400", "Children": null }, { "RegionName": "市中区", "RegionCode": "370402", "ParentCode": "370400", "Children": null }, { "RegionName": "薛城区", "RegionCode": "370403", "ParentCode": "370400", "Children": null }, { "RegionName": "峄城区", "RegionCode": "370404", "ParentCode": "370400", "Children": null }, { "RegionName": "台儿庄区", "RegionCode": "370405", "ParentCode": "370400", "Children": null }, { "RegionName": "山亭区", "RegionCode": "370406", "ParentCode": "370400", "Children": null }, { "RegionName": "滕州市", "RegionCode": "370481", "ParentCode": "370400", "Children": null } ] }, { "RegionName": "东营市", "RegionCode": "370500", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370501", "ParentCode": "370500", "Children": null }, { "RegionName": "东营区", "RegionCode": "370502", "ParentCode": "370500", "Children": null }, { "RegionName": "河口区", "RegionCode": "370503", "ParentCode": "370500", "Children": null }, { "RegionName": "垦利县", "RegionCode": "370521", "ParentCode": "370500", "Children": null }, { "RegionName": "利津县", "RegionCode": "370522", "ParentCode": "370500", "Children": null }, { "RegionName": "广饶县", "RegionCode": "370523", "ParentCode": "370500", "Children": null } ] }, { "RegionName": "烟台市", "RegionCode": "370600", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370601", "ParentCode": "370600", "Children": null }, { "RegionName": "芝罘区", "RegionCode": "370602", "ParentCode": "370600", "Children": null }, { "RegionName": "福山区", "RegionCode": "370611", "ParentCode": "370600", "Children": null }, { "RegionName": "牟平区", "RegionCode": "370612", "ParentCode": "370600", "Children": null }, { "RegionName": "莱山区", "RegionCode": "370613", "ParentCode": "370600", "Children": null }, { "RegionName": "长岛县", "RegionCode": "370634", "ParentCode": "370600", "Children": null }, { "RegionName": "龙口市", "RegionCode": "370681", "ParentCode": "370600", "Children": null }, { "RegionName": "莱阳市", "RegionCode": "370682", "ParentCode": "370600", "Children": null }, { "RegionName": "莱州市", "RegionCode": "370683", "ParentCode": "370600", "Children": null }, { "RegionName": "蓬莱市", "RegionCode": "370684", "ParentCode": "370600", "Children": null }, { "RegionName": "招远市", "RegionCode": "370685", "ParentCode": "370600", "Children": null }, { "RegionName": "栖霞市", "RegionCode": "370686", "ParentCode": "370600", "Children": null }, { "RegionName": "海阳市", "RegionCode": "370687", "ParentCode": "370600", "Children": null } ] }, { "RegionName": "潍坊市", "RegionCode": "370700", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370701", "ParentCode": "370700", "Children": null }, { "RegionName": "潍城区", "RegionCode": "370702", "ParentCode": "370700", "Children": null }, { "RegionName": "寒亭区", "RegionCode": "370703", "ParentCode": "370700", "Children": null }, { "RegionName": "坊子区", "RegionCode": "370704", "ParentCode": "370700", "Children": null }, { "RegionName": "奎文区", "RegionCode": "370705", "ParentCode": "370700", "Children": null }, { "RegionName": "临朐县", "RegionCode": "370724", "ParentCode": "370700", "Children": null }, { "RegionName": "昌乐县", "RegionCode": "370725", "ParentCode": "370700", "Children": null }, { "RegionName": "青州市", "RegionCode": "370781", "ParentCode": "370700", "Children": null }, { "RegionName": "诸城市", "RegionCode": "370782", "ParentCode": "370700", "Children": null }, { "RegionName": "寿光市", "RegionCode": "370783", "ParentCode": "370700", "Children": null }, { "RegionName": "安丘市", "RegionCode": "370784", "ParentCode": "370700", "Children": null }, { "RegionName": "高密市", "RegionCode": "370785", "ParentCode": "370700", "Children": null }, { "RegionName": "昌邑市", "RegionCode": "370786", "ParentCode": "370700", "Children": null } ] }, { "RegionName": "济宁市", "RegionCode": "370800", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370801", "ParentCode": "370800", "Children": null }, { "RegionName": "任城区", "RegionCode": "370811", "ParentCode": "370800", "Children": null }, { "RegionName": "兖州区", "RegionCode": "370812", "ParentCode": "370800", "Children": null }, { "RegionName": "微山县", "RegionCode": "370826", "ParentCode": "370800", "Children": null }, { "RegionName": "鱼台县", "RegionCode": "370827", "ParentCode": "370800", "Children": null }, { "RegionName": "金乡县", "RegionCode": "370828", "ParentCode": "370800", "Children": null }, { "RegionName": "嘉祥县", "RegionCode": "370829", "ParentCode": "370800", "Children": null }, { "RegionName": "汶上县", "RegionCode": "370830", "ParentCode": "370800", "Children": null }, { "RegionName": "泗水县", "RegionCode": "370831", "ParentCode": "370800", "Children": null }, { "RegionName": "梁山县", "RegionCode": "370832", "ParentCode": "370800", "Children": null }, { "RegionName": "曲阜市", "RegionCode": "370881", "ParentCode": "370800", "Children": null }, { "RegionName": "邹城市", "RegionCode": "370883", "ParentCode": "370800", "Children": null } ] }, { "RegionName": "泰安市", "RegionCode": "370900", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "370901", "ParentCode": "370900", "Children": null }, { "RegionName": "泰山区", "RegionCode": "370902", "ParentCode": "370900", "Children": null }, { "RegionName": "岱岳区", "RegionCode": "370911", "ParentCode": "370900", "Children": null }, { "RegionName": "宁阳县", "RegionCode": "370921", "ParentCode": "370900", "Children": null }, { "RegionName": "东平县", "RegionCode": "370923", "ParentCode": "370900", "Children": null }, { "RegionName": "新泰市", "RegionCode": "370982", "ParentCode": "370900", "Children": null }, { "RegionName": "肥城市", "RegionCode": "370983", "ParentCode": "370900", "Children": null } ] }, { "RegionName": "威海市", "RegionCode": "371000", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371001", "ParentCode": "371000", "Children": null }, { "RegionName": "环翠区", "RegionCode": "371002", "ParentCode": "371000", "Children": null }, { "RegionName": "文登区", "RegionCode": "371003", "ParentCode": "371000", "Children": null }, { "RegionName": "荣成市", "RegionCode": "371082", "ParentCode": "371000", "Children": null }, { "RegionName": "乳山市", "RegionCode": "371083", "ParentCode": "371000", "Children": null } ] }, { "RegionName": "日照市", "RegionCode": "371100", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371101", "ParentCode": "371100", "Children": null }, { "RegionName": "东港区", "RegionCode": "371102", "ParentCode": "371100", "Children": null }, { "RegionName": "岚山区", "RegionCode": "371103", "ParentCode": "371100", "Children": null }, { "RegionName": "五莲县", "RegionCode": "371121", "ParentCode": "371100", "Children": null }, { "RegionName": "莒县", "RegionCode": "371122", "ParentCode": "371100", "Children": null } ] }, { "RegionName": "莱芜市", "RegionCode": "371200", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371201", "ParentCode": "371200", "Children": null }, { "RegionName": "莱城区", "RegionCode": "371202", "ParentCode": "371200", "Children": null }, { "RegionName": "钢城区", "RegionCode": "371203", "ParentCode": "371200", "Children": null } ] }, { "RegionName": "临沂市", "RegionCode": "371300", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371301", "ParentCode": "371300", "Children": null }, { "RegionName": "兰山区", "RegionCode": "371302", "ParentCode": "371300", "Children": null }, { "RegionName": "罗庄区", "RegionCode": "371311", "ParentCode": "371300", "Children": null }, { "RegionName": "河东区", "RegionCode": "371312", "ParentCode": "371300", "Children": null }, { "RegionName": "沂南县", "RegionCode": "371321", "ParentCode": "371300", "Children": null }, { "RegionName": "郯城县", "RegionCode": "371322", "ParentCode": "371300", "Children": null }, { "RegionName": "沂水县", "RegionCode": "371323", "ParentCode": "371300", "Children": null }, { "RegionName": "兰陵县", "RegionCode": "371324", "ParentCode": "371300", "Children": null }, { "RegionName": "费县", "RegionCode": "371325", "ParentCode": "371300", "Children": null }, { "RegionName": "平邑县", "RegionCode": "371326", "ParentCode": "371300", "Children": null }, { "RegionName": "莒南县", "RegionCode": "371327", "ParentCode": "371300", "Children": null }, { "RegionName": "蒙阴县", "RegionCode": "371328", "ParentCode": "371300", "Children": null }, { "RegionName": "临沭县", "RegionCode": "371329", "ParentCode": "371300", "Children": null } ] }, { "RegionName": "德州市", "RegionCode": "371400", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371401", "ParentCode": "371400", "Children": null }, { "RegionName": "德城区", "RegionCode": "371402", "ParentCode": "371400", "Children": null }, { "RegionName": "陵城区", "RegionCode": "371403", "ParentCode": "371400", "Children": null }, { "RegionName": "宁津县", "RegionCode": "371422", "ParentCode": "371400", "Children": null }, { "RegionName": "庆云县", "RegionCode": "371423", "ParentCode": "371400", "Children": null }, { "RegionName": "临邑县", "RegionCode": "371424", "ParentCode": "371400", "Children": null }, { "RegionName": "齐河县", "RegionCode": "371425", "ParentCode": "371400", "Children": null }, { "RegionName": "平原县", "RegionCode": "371426", "ParentCode": "371400", "Children": null }, { "RegionName": "夏津县", "RegionCode": "371427", "ParentCode": "371400", "Children": null }, { "RegionName": "武城县", "RegionCode": "371428", "ParentCode": "371400", "Children": null }, { "RegionName": "乐陵市", "RegionCode": "371481", "ParentCode": "371400", "Children": null }, { "RegionName": "禹城市", "RegionCode": "371482", "ParentCode": "371400", "Children": null } ] }, { "RegionName": "聊城市", "RegionCode": "371500", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371501", "ParentCode": "371500", "Children": null }, { "RegionName": "东昌府区", "RegionCode": "371502", "ParentCode": "371500", "Children": null }, { "RegionName": "阳谷县", "RegionCode": "371521", "ParentCode": "371500", "Children": null }, { "RegionName": "莘县", "RegionCode": "371522", "ParentCode": "371500", "Children": null }, { "RegionName": "茌平县", "RegionCode": "371523", "ParentCode": "371500", "Children": null }, { "RegionName": "东阿县", "RegionCode": "371524", "ParentCode": "371500", "Children": null }, { "RegionName": "冠县", "RegionCode": "371525", "ParentCode": "371500", "Children": null }, { "RegionName": "高唐县", "RegionCode": "371526", "ParentCode": "371500", "Children": null }, { "RegionName": "临清市", "RegionCode": "371581", "ParentCode": "371500", "Children": null } ] }, { "RegionName": "滨州市", "RegionCode": "371600", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371601", "ParentCode": "371600", "Children": null }, { "RegionName": "滨城区", "RegionCode": "371602", "ParentCode": "371600", "Children": null }, { "RegionName": "沾化区", "RegionCode": "371603", "ParentCode": "371600", "Children": null }, { "RegionName": "惠民县", "RegionCode": "371621", "ParentCode": "371600", "Children": null }, { "RegionName": "阳信县", "RegionCode": "371622", "ParentCode": "371600", "Children": null }, { "RegionName": "无棣县", "RegionCode": "371623", "ParentCode": "371600", "Children": null }, { "RegionName": "博兴县", "RegionCode": "371625", "ParentCode": "371600", "Children": null }, { "RegionName": "邹平县", "RegionCode": "371626", "ParentCode": "371600", "Children": null } ] }, { "RegionName": "菏泽市", "RegionCode": "371700", "ParentCode": "370000", "Children": [ { "RegionName": "市辖区", "RegionCode": "371701", "ParentCode": "371700", "Children": null }, { "RegionName": "牡丹区", "RegionCode": "371702", "ParentCode": "371700", "Children": null }, { "RegionName": "曹县", "RegionCode": "371721", "ParentCode": "371700", "Children": null }, { "RegionName": "单县", "RegionCode": "371722", "ParentCode": "371700", "Children": null }, { "RegionName": "成武县", "RegionCode": "371723", "ParentCode": "371700", "Children": null }, { "RegionName": "巨野县", "RegionCode": "371724", "ParentCode": "371700", "Children": null }, { "RegionName": "郓城县", "RegionCode": "371725", "ParentCode": "371700", "Children": null }, { "RegionName": "鄄城县", "RegionCode": "371726", "ParentCode": "371700", "Children": null }, { "RegionName": "定陶县", "RegionCode": "371727", "ParentCode": "371700", "Children": null }, { "RegionName": "东明县", "RegionCode": "371728", "ParentCode": "371700", "Children": null } ] } ] } }
使用过多的递归出现错误,“System.StackOverflowException”,由于递归数据放在栈里,递归不断的压栈,.NET framework Runtime 就抛出异常。
递归算法解题的运行效率较低。在递归调用的过程当中系统为每一层的返回点、局部量等开辟了栈来存储。递归次数过多容易造成栈溢出等。