使用 LINQ 使用嵌套数组从 class 获取子值和父值 属性
Get Child and Parent property values from class with nested arrays using LINQ
所以另一个 linq 问题。
有什么方法可以通过仅知道最深嵌套子 属性 的值的 linq 提取子和父 属性 值?
为了扩展我的问题,这就是我想要做的:
我有这个,但版本更大。虽然基本的格式构造在这里是无法理解的。我将对象反序列化为 c# class:
{
"id": "ProductCatalogId1",
"name": "Product Catalog 1",
"result": {
"productFamily": [
{
"id": "FamilyId1",
"name": "Family 1",
"productCategory": [
{
"id": "CategoryId1",
"name": "Category 1",
"productConstruction": [
{
"id": "ConstructionId1",
"name": "Construction 1",
"productLine": [
{
"id": "LineId1",
"name": "Line 1",
"productModel": [
{
"id": "pModelId1",
"name": "pModel 1",
"customerModel": [
{
"id": "cModelId1",
"name": "cModel 1"
}
]
},
{
"id": "pModelId2",
"name": "pModel 2",
"customerModel": [
{
"id": "cModelId2",
"name": "cModel 2"
}
]
}]
}]
}]
}]
}]
}
}
Class(es) 与上述结构相关:
public class Productcatalog
{
public string id { get; set; }
public string name { get; set; }
public StructureResult result { get; set; }
}
public class StructureResult
{
public Productfamily[] productFamily { get; set; }
}
public class Productfamily
{
public string id { get; set; }
public string name { get; set; }
public Productcategory[] productCategory { get; set; }
public FulfilsKeyTechnology[] FulfilsKeyTechnology { get; set; }
}
public class Productcategory
{
public string id { get; set; }
public string name { get; set; }
public Productconstruction[] productConstruction { get; set; }
}
public class Productconstruction
{
public string id { get; set; }
public string name { get; set; }
public Productline[] productLine { get; set; }
}
public class Productline
{
public string id { get; set; }
public string name { get; set; }
public Productmodel[] productModel { get; set; }
}
public class Productmodel
{
public string id { get; set; }
public string name { get; set; }
public Customermodel[] customerModel { get; set; }
}
public class Customermodel
{
public string id { get; set; }
public string name { get; set; }
}
我想要做的是在最后得到一个看起来像这样的列表,从具有相同属性的 class 序列化:
[
{
"productConstructionId":"ConstructionId1",
"productConstructionName":"Construction 1",
"productLineId":"LineId1",
"productLineName":"Line 1",
"customerModelId":"cModelId1",
"customerModelName":"cModel 1"
},
{
"productConstructionId":"ConstructionId1",
"productConstructionName":"Construction 1",
"productLineId":"LineId1",
"productLineName":"Line 1",
"customerModelId":"cModelId2",
"customerModelName":"cModel 2"
}
]
Class 与上述结构相关:
public class ModelList
{
public string productConstructionId {get; set;}
public string productConstructionName {get; set;}
public string productLineId {get; set;}
public string productLineName {get; set;}
public string customerModelId {get; set;}
public string customerModelName {get; set;}
}
也许在这种情况下 linq 甚至不是最佳实践?
试试这个
var jsonDeserialized = JsonConvert.DeserializeObject<Data>(json);
var list = new List<ModelList>();
foreach (var pf in jsonDeserialized.Result.ProductFamily)
{
foreach (var pct in pf.ProductCategory)
{
foreach (var pcn in pct.ProductConstruction)
{
foreach (var pl in pcn.ProductLine)
{
foreach (var pm in pl.ProductModel)
{
foreach (var cm in pm.CustomerModel)
{
var item = new ModelList
{
ProductFamilyId = pf.Id,
ProducFamilyName = pf.Name,
ProductCategoryId = pct.Id,
ProductCategoryName = pct.Name,
ProductConstructionId = pcn.Id,
ProductConstructionName = pcn.Name,
ProductLineId = pl.Id,
ProductLineName = pl.Name,
CustomerModelId = cm.Id,
CustomerModelName = cm.Name
};
list.Add(item);
}
}
}
}
}
}
输出
[
{
"ProductFamilyId": "FamilyId1",
"ProducFamilyName": "Family 1",
"ProductCategoryId": "CategoryId1",
"ProductCategoryName": "Category 1",
"ProductConstructionId": "ConstructionId1",
"ProductConstructionName": "Construction 1",
"ProductLineId": "LineId1",
"ProductLineName": "Line 1",
"CustomerModelId": "cModelId1",
"CustomerModelName": "cModel 1"
},
{
"ProductFamilyId": "FamilyId1",
"ProducFamilyName": "Family 1",
"ProductCategoryId": "CategoryId1",
"ProductCategoryName": "Category 1",
"ProductConstructionId": "ConstructionId1",
"ProductConstructionName": "Construction 1",
"ProductLineId": "LineId1",
"ProductLineName": "Line 1",
"CustomerModelId": "cModelId2",
"CustomerModelName": "cModel 2"
}
]
类
public class ModelList
{
public string ProductFamilyId { get; set; }
public string ProducFamilyName { get; set; }
public string ProductCategoryId { get; set; }
public string ProductCategoryName { get; set; }
public string ProductConstructionId { get; set; }
public string ProductConstructionName { get; set; }
public string ProductLineId { get; set; }
public string ProductLineName { get; set; }
public string CustomerModelId { get; set; }
public string CustomerModelName { get; set; }
}
public partial class Data
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("result")]
public Result Result { get; set; }
}
public partial class Result
{
[JsonProperty("productFamily")]
public ProductFamily[] ProductFamily { get; set; }
}
public partial class ProductFamily
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productCategory")]
public ProductCategory[] ProductCategory { get; set; }
}
public partial class ProductCategory
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productConstruction")]
public ProductConstruction[] ProductConstruction { get; set; }
}
public partial class ProductConstruction
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productLine")]
public ProductLine[] ProductLine { get; set; }
}
public partial class ProductLine
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productModel")]
public ProductModel[] ProductModel { get; set; }
}
public partial class ProductModel
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("customerModel")]
public CustomerModel[] CustomerModel { get; set; }
}
public partial class CustomerModel
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
所以另一个 linq 问题。
有什么方法可以通过仅知道最深嵌套子 属性 的值的 linq 提取子和父 属性 值?
为了扩展我的问题,这就是我想要做的:
我有这个,但版本更大。虽然基本的格式构造在这里是无法理解的。我将对象反序列化为 c# class:
{
"id": "ProductCatalogId1",
"name": "Product Catalog 1",
"result": {
"productFamily": [
{
"id": "FamilyId1",
"name": "Family 1",
"productCategory": [
{
"id": "CategoryId1",
"name": "Category 1",
"productConstruction": [
{
"id": "ConstructionId1",
"name": "Construction 1",
"productLine": [
{
"id": "LineId1",
"name": "Line 1",
"productModel": [
{
"id": "pModelId1",
"name": "pModel 1",
"customerModel": [
{
"id": "cModelId1",
"name": "cModel 1"
}
]
},
{
"id": "pModelId2",
"name": "pModel 2",
"customerModel": [
{
"id": "cModelId2",
"name": "cModel 2"
}
]
}]
}]
}]
}]
}]
}
}
Class(es) 与上述结构相关:
public class Productcatalog
{
public string id { get; set; }
public string name { get; set; }
public StructureResult result { get; set; }
}
public class StructureResult
{
public Productfamily[] productFamily { get; set; }
}
public class Productfamily
{
public string id { get; set; }
public string name { get; set; }
public Productcategory[] productCategory { get; set; }
public FulfilsKeyTechnology[] FulfilsKeyTechnology { get; set; }
}
public class Productcategory
{
public string id { get; set; }
public string name { get; set; }
public Productconstruction[] productConstruction { get; set; }
}
public class Productconstruction
{
public string id { get; set; }
public string name { get; set; }
public Productline[] productLine { get; set; }
}
public class Productline
{
public string id { get; set; }
public string name { get; set; }
public Productmodel[] productModel { get; set; }
}
public class Productmodel
{
public string id { get; set; }
public string name { get; set; }
public Customermodel[] customerModel { get; set; }
}
public class Customermodel
{
public string id { get; set; }
public string name { get; set; }
}
我想要做的是在最后得到一个看起来像这样的列表,从具有相同属性的 class 序列化:
[
{
"productConstructionId":"ConstructionId1",
"productConstructionName":"Construction 1",
"productLineId":"LineId1",
"productLineName":"Line 1",
"customerModelId":"cModelId1",
"customerModelName":"cModel 1"
},
{
"productConstructionId":"ConstructionId1",
"productConstructionName":"Construction 1",
"productLineId":"LineId1",
"productLineName":"Line 1",
"customerModelId":"cModelId2",
"customerModelName":"cModel 2"
}
]
Class 与上述结构相关:
public class ModelList
{
public string productConstructionId {get; set;}
public string productConstructionName {get; set;}
public string productLineId {get; set;}
public string productLineName {get; set;}
public string customerModelId {get; set;}
public string customerModelName {get; set;}
}
也许在这种情况下 linq 甚至不是最佳实践?
试试这个
var jsonDeserialized = JsonConvert.DeserializeObject<Data>(json);
var list = new List<ModelList>();
foreach (var pf in jsonDeserialized.Result.ProductFamily)
{
foreach (var pct in pf.ProductCategory)
{
foreach (var pcn in pct.ProductConstruction)
{
foreach (var pl in pcn.ProductLine)
{
foreach (var pm in pl.ProductModel)
{
foreach (var cm in pm.CustomerModel)
{
var item = new ModelList
{
ProductFamilyId = pf.Id,
ProducFamilyName = pf.Name,
ProductCategoryId = pct.Id,
ProductCategoryName = pct.Name,
ProductConstructionId = pcn.Id,
ProductConstructionName = pcn.Name,
ProductLineId = pl.Id,
ProductLineName = pl.Name,
CustomerModelId = cm.Id,
CustomerModelName = cm.Name
};
list.Add(item);
}
}
}
}
}
}
输出
[
{
"ProductFamilyId": "FamilyId1",
"ProducFamilyName": "Family 1",
"ProductCategoryId": "CategoryId1",
"ProductCategoryName": "Category 1",
"ProductConstructionId": "ConstructionId1",
"ProductConstructionName": "Construction 1",
"ProductLineId": "LineId1",
"ProductLineName": "Line 1",
"CustomerModelId": "cModelId1",
"CustomerModelName": "cModel 1"
},
{
"ProductFamilyId": "FamilyId1",
"ProducFamilyName": "Family 1",
"ProductCategoryId": "CategoryId1",
"ProductCategoryName": "Category 1",
"ProductConstructionId": "ConstructionId1",
"ProductConstructionName": "Construction 1",
"ProductLineId": "LineId1",
"ProductLineName": "Line 1",
"CustomerModelId": "cModelId2",
"CustomerModelName": "cModel 2"
}
]
类
public class ModelList
{
public string ProductFamilyId { get; set; }
public string ProducFamilyName { get; set; }
public string ProductCategoryId { get; set; }
public string ProductCategoryName { get; set; }
public string ProductConstructionId { get; set; }
public string ProductConstructionName { get; set; }
public string ProductLineId { get; set; }
public string ProductLineName { get; set; }
public string CustomerModelId { get; set; }
public string CustomerModelName { get; set; }
}
public partial class Data
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("result")]
public Result Result { get; set; }
}
public partial class Result
{
[JsonProperty("productFamily")]
public ProductFamily[] ProductFamily { get; set; }
}
public partial class ProductFamily
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productCategory")]
public ProductCategory[] ProductCategory { get; set; }
}
public partial class ProductCategory
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productConstruction")]
public ProductConstruction[] ProductConstruction { get; set; }
}
public partial class ProductConstruction
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productLine")]
public ProductLine[] ProductLine { get; set; }
}
public partial class ProductLine
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("productModel")]
public ProductModel[] ProductModel { get; set; }
}
public partial class ProductModel
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("customerModel")]
public CustomerModel[] CustomerModel { get; set; }
}
public partial class CustomerModel
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}