【问题标题】:Create hierarchical object in c#在 C# 中创建分层对象
【发布时间】:2021-12-01 11:48:29
【问题描述】:

我有一个示例数据,如下所示,它在运行时会有所不同(可以添加或删除行和列):

Column1   Column2  Column3
RED       Car      100
RED       Car      150
RED       Bike     140
BLUE      Car      240
BLUE      Bike     120

我的班级如下:

 public class Data{
       public List<Data> ChildObjects;
       public string strName;
    }

我想创建上述示例数据的对象,如下所示:

  • parentObj 将包含 2 个 ChildObjects-RED 和 BLUE 以及 strName 作为
    “父母”
  • RED obj 将包含 2 个 ChildObjects CAR 和 BIKE 以及 strName 作为 “红色”
  • BLUE obj 将包含 2 个 ChildObjects CAR 和 BIKE 以及 strName 作为 “蓝色”
  • 每个 CAR 对象将包含 5 个 ChildObjects (100,150,140,​​240,120) strName 为“CAR”
  • 每个 BIKE 对象将包含 5 个 ChildObjects (100,150,140,​​240,120) strName 为“BIKE”

所以结构看起来像这样

                100
                150
       Car  --  140
                240
               120
RED---
               100
               150
      Bike --  140
               240
               120


                100
                150
       Car  --  140
      |         240
      |         120
BLUE---
      |        100
      |        150
      Bike --  140
               240
               120

请帮我为逻辑编写高效且动态的代码sn-p。

【问题讨论】:

    标签: c# object logic


    【解决方案1】:
     var parentObj = new Data
                {
                    strName = "parent",
                    ChildObjects = new List<Data>
                    {
                        // Creation of RED
                        new Data
                        {
                            strName = "RED",
                            ChildObjects = new List<Data>
                            {
                                // Creation of Car
                                new Data
                                {
                                    strName = "Car",
                                    ChildObjects = new List<Data>
                                    {
                                        new Data {strName = "100"},
                                        new Data {strName = "150"},
                                        new Data {strName = "140"},
                                        new Data {strName = "240"},
                                        new Data {strName = "120"}
                                    }
                                },
                                // Creation of Bike
                                new Data
                                {
                                    strName = "Bike",
                                    ChildObjects = new List<Data>
                                    {
                                        new Data {strName = "100"},
                                        new Data {strName = "150"},
                                        new Data {strName = "140"},
                                        new Data {strName = "240"},
                                        new Data {strName = "120"}
                                    }
                                }
                            }
                        },
                        // Creation of BLUE
                        new Data
                        {
                            strName = "BLUE",
                            ChildObjects = new List<Data>
                            {
                                // Creation of Car
                                new Data
                                {
                                    strName = "Car",
                                    ChildObjects = new List<Data>
                                    {
                                        new Data {strName = "100"},
                                        new Data {strName = "150"},
                                        new Data {strName = "140"},
                                        new Data {strName = "240"},
                                        new Data {strName = "120"}
                                    }
                                },
                                // Creation of Bike
                                new Data
                                {
                                    strName = "Bike",
                                    ChildObjects = new List<Data>
                                    {
                                        new Data {strName = "100"},
                                        new Data {strName = "150"},
                                        new Data {strName = "140"},
                                        new Data {strName = "240"},
                                        new Data {strName = "120"}
                                    }
                                }
                            }
                        }
                    }
                };
    

    【讨论】:

    • 感谢您的帮助。但我需要样本的动态逻辑。我不知道样本中的列和行以及数据。它是在运行时给出的。
    • 我不明白这张表是如何变成树的逻辑
    • 您好,我想要与您提供的代码完全相同的内容。但在运行时,我不知道示例数据将包含多少行和哪些值。所以对象应该是动态创建的。
    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多