Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ展平嵌套结构_C#_Linq - Fatal编程技术网

C# 使用LINQ展平嵌套结构

C# 使用LINQ展平嵌套结构,c#,linq,C#,Linq,我收到一个JSON负载,它有嵌套列表。LINQ是否可以用于展平结构并将嵌套列表提取为非规范化形式 我有很强的数据库背景,所以我称之为JSON数据的非规范化形式 我以前使用过LINQ,但没有使用这些深层结构 我尝试过使用LINQ fluent方法,但我可以;我似乎无法进入嵌套列表 public class Exampleobject { public string result { get; set; } publi

我收到一个JSON负载,它有嵌套列表。LINQ是否可以用于展平结构并将嵌套列表提取为非规范化形式

我有很强的数据库背景,所以我称之为JSON数据的非规范化形式

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

我尝试过使用LINQ fluent方法,但我可以;我似乎无法进入嵌套列表


        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; }
        }                     


我像这样加载json数据
var resulting=JsonConvert.DeserializeObject(rawjson);
使用查询语法:

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 = resulting.propertyData.building
    .SelectMany(building => building.floorsInvolved
        .Select(floor => $"{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
    select $"{building.itemId} {building.buildingTypeDescription} " +
        $"{floor.InvolvedId} " +
        $"{string.Join(", ", floor.roles.Select(role => role.description))}";
或者(也许更容易理解):

使用扩展方法语法:

 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))}"));

扩展方法语法中的
SelectMany
,或查询语法中的
from x in xs from y in x.ys…
。我已经使用了SelectMany,但我仍然无法找到在这种情况下如何正确使用LINQ的答案
SelectMany
在扩展方法语法中,或者查询语法中的
from x in xs from y in x.ys…
。我已经使用了SelectMany,但我仍然无法找到在这种情况下如何正确使用LINQ的答案Beautiful——工作起来很有魅力,我终于对如何在这些数据上使用LINQ有了一些见解。我欠你很多啤酒!漂亮——工作起来很有魅力,我终于有了一些见解,可以继续在这些数据上使用LINQ。我欠你很多啤酒!
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))}"));