【问题标题】:Array of objects within an array of objects对象数组中的对象数组
【发布时间】:2018-08-17 02:01:51
【问题描述】:

我想创建一个对象数组,但我觉得这很困难,因为我只是使用 C# 的初学者。我的对象数组非常复杂,因为我在对象中有一个元素,它也需要像这样的对象数组。

array = [
    {
      "bch": "001",
      "branch": "AAA",
      "pdaccounts": [
            {"Name":"John Doe","Amount":1000.00},...
      ],
      "raccounts": [
            {"Name":"John Doess","Amount":1980.56},...
      ],
      "collapse":true
    },
    ...
];

谁能帮帮我?

【问题讨论】:

  • 你能展示一下你到目前为止所做的尝试吗?您是否为这些定义了类,还是打算将它们设为匿名类型?
  • 是的,我为 pdaccountsraccounts 定义了用于制作对象的类。我传递了这些创建的对象并使用 javascript 创建了最终数组,但这非常耗时,因为我在 pdaccountsraccounts 中提取了单独的数据。我想要的是在我的控制器中以一种方法创建最终数组。先生可以吗?
  • 这看起来像是尝试在 C# 中使用 javascript 或 php 成语。不会很顺利。
  • 不,乔尔不是这么说的。 Joel 说您似乎正在尝试采用 javascript/php 编码实践并将它们应用于 C#。由于 C# 与 javascript/php 完全不同,因此您应该学习 C# 中的典型处理方式。

标签: c# arrays object


【解决方案1】:

您可以通过多种方式初始化列表和嵌套列表。想象这是我的课。我已经包含了一个用于对象初始值设定项模式的无参数构造函数(尽管它不限于无参数构造函数),一个带有名称和列表对象的构造函数,以及一个带有名称和 params 数组的构造函数节点数。

为了简单起见,我自己做了类引用,但显然你不需要这样做。

public class Node
{
    public Node() // you could pass nothing and set them manually or use the object initializer pattern
    {
    }

    public Node(string name, List<Node> nodes) // you could pass the name and an existing list
    {
        this.Name = name;
        this.Nodes = nodes;
    }

    public Node(string name, params Node[] nodes) // you could pass the name and a list of items (which can be called like Node("a", node, node, node)
    {
        this.Name = name;
        this.Nodes = nodes.ToList(); // needs    using System.Linq;    at the top of the file or namespace
    }

    public string Name { get; set; }
    public List<Node> Nodes { get; set; }
}

例子:

// object initializer
var childChildChildChildNode = new Node
{
    Name = "ChildChildChildChild"
}

// constructor: string name, params Node[] nodes
var childChildChildNode = new Node("ChildChildChild", childChildChildChildNode);

// constructor: string name, List<Node> nodes
var childChildNode = new Node("ChildChild", new List<Node> { childChildChildNode });

// object initializer
var childNode = new Node
{
    Name = "Child",
    Nodes = new List<Node>()
};
// add items to the list of the child node
childNode.Nodes.Add(childChildNode);

// object initializer for node and list
var parentNode = new Node
{
    Name = "Parent",
    Nodes = new List<Node> { childNode }
};

// full object initializer
var otherParentNode = new Node
{
    Name = "Parent",
    Nodes = new List<Node>
    {
        new Node {
            Name = "Child",
            Nodes = new List<Node>
            {
                new Node
                {
                    Name = "ChildChild1"
                },
                new Node
                {
                    Name = "ChildChild2"
                }
            }
        }
    }
};

请注意,这并不是使用数组和嵌套数组初始化对象的方法的详尽列表,只是一些示例来帮助您入门。

【讨论】:

  • 这是我正在寻找的东西,但不幸的是我在互联网上找不到它。谢谢@John先生,我将把它作为我的基础。
【解决方案2】:

考虑您需要在包含此信息的代码中表示的类/对象。你可能已经习惯了这样的 POCO 类:

public class Human 
{
    public string Name { get; set; }
    public int Age { get; set; }
}

像上面人类示例这样的类可以包含您定义的其他类。请注意下面 CheeseBurger 类中的 Cheese 属性。

public class Cheese
{
    public int SmellFactor { get; set; }
}

public class CheeseBurger
{
    public Cheese CheeseType { get; set; }
}

这可以很容易地适用于适合您的每个属性“pdaccounts”和“raccounts”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2015-11-13
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多