【发布时间】:2021-12-19 14:37:07
【问题描述】:
所以又是一个 linq 问题。
有没有办法通过 linq 只知道嵌套最深的子属性的值来提取子属性值和父属性值?
要扩展我的问题,这就是我想要做的:
我有这个,但作为一个更大的版本。虽然基本的格式结构可以在这里理解。我将对象反序列化为 c# 类:
{
"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"
}
]
}]
}]
}]
}]
}]
}
}
与上述结构相关的类:
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; }
}
我想要做的是在最后得到一个看起来像这样的列表,从具有相同属性的类序列化:
[
{
"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"
}
]
与上述结构相关的类:
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 甚至不是最佳实践?
【问题讨论】:
-
请您也发布您的两个课程吗?
-
@Serge 好了。
标签: c# linq .net-core nested-lists