关于树形表结构的数据如何返回级联数据结构
/// <summary>
/// 数据组装
/// </summary>
/// <param name="regionTypeData">开始级数据</param>
/// <param name="source">总省市区数据</param>
/// <param name="endRegion">结束级 默认区(含)1国2省3市4区</param>
/// <returns></returns>
public List<City_OM> GetCityRange(List<City_OM> regionTypeData, List<City_OM> source, int endRegion = 4)
{
if (!regionTypeData.Any() || int.Parse(regionTypeData.First().RegionType) > endRegion)
{
return new List<City_OM>();
}
List<City_OM> data = new List<City_OM>();
regionTypeData.ForEach(f =>
{
data.Add(new City_OM
{
Code = f.Code,
Name = f.Name,
ParentCode = f.ParentCode,
RegionType = f.RegionType,
Children = GetCityRange(source.Where(w => w.ParentCode == f.Code).ToList(), source, endRegion)
});
});
return data;
}
实体
/// <summary>
/// 获取省市区
/// </summary>
public class City_OM
{
/// <summary>
/// id
/// </summary>
public int Code { get; set; }
/// <summary>
/// 上级id
/// </summary>
public int ParentCode { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 级别 1 国家 2 省 3 市 4 区县
/// </summary>
public string RegionType { get; set; }
/// <summary>
///
/// </summary>
public List<City_OM> Children { get; set; }
}
以宁夏回族自治区为例,返回:
{
"code": 640000,
"parentCode": 1,
"name": "宁夏回族自治区",
"regionType": "2",
"children": [
{
"code": 640100,
"parentCode": 640000,
"name": "银川市",
"regionType": "3",
"children": [
{
"code": 640104,
"parentCode": 640100,
"name": "兴庆区",
"regionType": "4",
"children": []
},
{
"code": 640105,
"parentCode": 640100,
"name": "西夏区",
"regionType": "4",
"children": []
},
{
"code": 640106,
"parentCode": 640100,
"name": "金凤区",
"regionType": "4",
"children": []
},
{
"code": 640121,
"parentCode": 640100,
"name": "永宁县",
"regionType": "4",
"children": []
},
{
"code": 640122,
"parentCode": 640100,
"name": "贺兰县",
"regionType": "4",
"children": []
},
{
"code": 640181,
"parentCode": 640100,
"name": "灵武市",
"regionType": "4",
"children": []
},
{
"code": 640182,
"parentCode": 640100,
"name": "其它区",
"regionType": "4",
"children": []
}
]
},
{
"code": 640200,
"parentCode": 640000,
"name": "石嘴山市",
"regionType": "3",
"children": [
{
"code": 640202,
"parentCode": 640200,
"name": "大武口区",
"regionType": "4",
"children": []
},
{
"code": 640205,
"parentCode": 640200,
"name": "惠农区",
"regionType": "4",
"children": []
},
{
"code": 640221,
"parentCode": 640200,
"name": "平罗县",
"regionType": "4",
"children": []
},
{
"code": 640222,
"parentCode": 640200,
"name": "其它区",
"regionType": "4",
"children": []
}
]
},
{
"code": 640300,
"parentCode": 640000,
"name": "吴忠市",
"regionType": "3",
"children": [
{
"code": 640302,
"parentCode": 640300,
"name": "利通区",
"regionType": "4",
"children": []
},
{
"code": 640303,
"parentCode": 640300,
"name": "红寺堡区",
"regionType": "4",
"children": []
},
{
"code": 640323,
"parentCode": 640300,
"name": "盐池县",
"regionType": "4",
"children": []
},
{
"code": 640324,
"parentCode": 640300,
"name": "同心县",
"regionType": "4",
"children": []
},
{
"code": 640381,
"parentCode": 640300,
"name": "青铜峡市",
"regionType": "4",
"children": []
},
{
"code": 640382,
"parentCode": 640300,
"name": "其它区",
"regionType": "4",
"children": []
}
]
},
{
"code": 640400,
"parentCode": 640000,
"name": "固原市",
"regionType": "3",
"children": [
{
"code": 640402,
"parentCode": 640400,
"name": "原州区",
"regionType": "4",
"children": []
},
{
"code": 640422,
"parentCode": 640400,
"name": "西吉县",
"regionType": "4",
"children": []
},
{
"code": 640423,
"parentCode": 640400,
"name": "隆德县",
"regionType": "4",
"children": []
},
{
"code": 640424,
"parentCode": 640400,
"name": "泾源县",
"regionType": "4",
"children": []
},
{
"code": 640425,
"parentCode": 640400,
"name": "彭阳县",
"regionType": "4",
"children": []
},
{
"code": 640426,
"parentCode": 640400,
"name": "其它区",
"regionType": "4",
"children": []
}
]
},
{
"code": 640500,
"parentCode": 640000,
"name": "中卫市",
"regionType": "3",
"children": [
{
"code": 640502,
"parentCode": 640500,
"name": "沙坡头区",
"regionType": "4",
"children": []
},
{
"code": 640521,
"parentCode": 640500,
"name": "中宁县",
"regionType": "4",
"children": []
},
{
"code": 640522,
"parentCode": 640500,
"name": "海原县",
"regionType": "4",
"children": []
},
{
"code": 640523,
"parentCode": 640500,
"name": "其它区",
"regionType": "4",
"children": []
}
]
}
]
}