使用LINQ展平嵌套结构

时间:2019-04-11 13:47:33

标签: c# linq

我正在接收具有嵌套列表的JSON有效负载。 LINQ可以用来平整结构并将嵌套列表拉成非规范化形式吗?

我有很强的数据库背景,这就是为什么我称它为JSON数据的非规范化形式。

我以前使用过LINQ,但没有使用过这些深层次的结构。

我尝试过使用LINQ流利方法,但我似乎无法进入嵌套列表


        public class Exampleobject
        {
            public string result { get; set; }
            public Propertydata propertyData { get; set; }
        }

        public class Propertydata
        {
            public List<Buildings> building { get; set; }           
        }



        public class Buildings
        {
            public string itemId { get; set; }
            [JsonProperty("type")]
            public string buildingType { get; set; }
            public string buildingTypeCode { get; set; }
            public string buildingTypeDescription { get; set; }            

            [JsonProperty("floors")]
            public List<floorInvolved> floorsInvolved { get; set; }            
        }

        public class floorInvolved
        {
            public string InvolvedId { get; set; }
            public List<FRole> roles { get; set; }
        }

        public class FRole
        {
            public string code { get; set; }
            public string description { get; set; }
        }                     


Sample Data:


{
"result": "200 OK",

"propertyData": {

"building": [


{
"itemId": "9A85B1CCBD65C1F2",
"type": "V",
"buildingTypeCode": "02",
"buildingTypeDescription": "mixed space",

"floors": [

{
"InvolvedId": "04",

"roles": [

{
"code": "OFF",
"description": "Office space"
},

{
"code": "APT",
"description": "Apartment"
},

{
"code": "STG",
"description": "Storage"
}
]
},
{
"InvolvedId": "05",

"roles": [

{
"code": "OFF",
"description": "Office space"
},


]
}
],

}
]
}
}
I'm trying to get the building bubbled up with the details like this:
ID                Description  Floor  Role
9A85B1CCBD65C1F2  mixed space    04   Office space, Apartment, Storage
9A85B1CCBD65C1F2  mixed space    05   Office space

I load the json data like so
var resulting = JsonConvert.DeserializeObject<Exampleobject>(rawjson);

1 个答案:

答案 0 :(得分:2)

使用查询语法:

var result =
    from building in resulting.propertyData.building
    from floor in building.floorsInvolved
    select $"{building.itemId} {building.buildingTypeDescription} " +
        $"{floor.InvolvedId} " +
        $"{string.Join(", ", floor.roles.Select(role => role.description))}";

或者(也许更可读):

var result =
    from building in resulting.propertyData.building
    from floor in building.floorsInvolved
    let roles = floor.roles.Select(role => role.description)
    select $"{building.itemId} {building.buildingTypeDescription} " +
        $"{floor.InvolvedId} {string.Join(", ", roles)}";

使用扩展方法语法:

 var result = resulting.propertyData.building
    .SelectMany(building => building.floorsInvolved
        .Select(floor => $"{building.itemId} {building.buildingTypeDescription} " +
            $"{floor.InvolvedId} " +
            $"{string.Join(", ", floor.roles.Select(role => role.description))}"));