簡體   English   中英

如何在 class 屬性內設置 object 數組

[英]How to set a array of object inner a class property

我需要創建一個 object 來返回一個 json ,如下所示:

{
        "Name": "John",
        "Age": 25,
        "Products": [
            {
                "Code": "1",
                "Price": 400.00
            },
            {
                "Code": "2",
                "Price": 100.00
            },
            {
                "Code": "32",
                "Price": 250.00
            },
        ],
}

我怎樣才能做到這一點? :/我只需要創建一個 object 並轉換為 json... 我知道如何轉換,但不知道如何使用“對象數組”填充產品 model

我的 Model Class:

public class Customer
{
    public string Name { get; set; }
    public string Age { get; set; }
    public Products[] Products { get; set; }
}

public class Products
{
    public string Code { get; set; }
    public string Price { get; set; }
}

我填充數據的程序:(在這里您可以看到一個示例填充客戶 model 10 次和產品 model 3 次)

            List<Object> customerList = new List<Object>();
            List<Object> productList = new List<Object>();

            for (int i = 0; i < 10; i++)
            {
                Customer customer = new Customer();

                customer.Name = "Pedro";
                customer.Age = "20";

                for (int ii = 0; ii < 3; ii++)
                {
                    Product product = new Product();

                    product.Code = "1";
                    product.Price = "400.00";

                    productList.Add(product);
                }

                customer.Product = // Why I need to do here?

                customerList.Add(customer);
}

您已經在列表中擁有產品,因此您只需將其轉換為數組以匹配屬性類型:

customer.Products = productList.ToArray();

當然,您應該將聲明更改為:

List<Product> productList = new List<Product>();

如果您出於任何原因不想更改聲明?:您可以這樣做:

customer.Products = productList.Select(x => (Product)x).ToArray();

對此的簡短回答:

customer.Product = // Why I need to do here?

只是您需要將您的產品添加為Product[] (因為這是在Customer類中定義的方式),我們可以通過將列表中的項目轉換為Product來做到這一點(因此我們獲得強類型對象而不僅僅是Object對象)並使用System.Linq擴展方法ToArray()從列表中創建一個數組:

customer.Product = productList.Select(p => (Product)p).ToArray();

// Note that we should clear our list for the next iteration
productsList.Clear();

還有一些需要考慮的事情:

  1. 類不應以復數命名,除非它們是 collections,因此您的Products class 應命名為Product

  2. 當您有可用的強類型類時,您應該避免使用object列表,因此List<Object> customerList應該是List<Customer> customers

  3. 您一遍又一遍地填充相同數據的列表。 我認為這是因為這只是一個示例,但想讓您知道以防萬一。

您現有的代碼可以修改為:

List<Customer> customers = new List<Customer>();

for (int i = 0; i < 10; i++)
{
    // Create a new customer
    Customer customer = new Customer
    {
        Name = "Pedro",
        Age = "20",
    };

    // Create an array of products since Customer expects this type
    Product[] products = new Product[3];
    for (int j = 0; j < products.Length; j++)
    {
        products[j] = new Product
        {
            Code = "1",
            Price = "400.00"
        };
    }

    // Add the array to our customer
    customer.Products = products;

    // Add our customer to the list
    customers.Add(customer);
}

或者我們可以使用純 Linq 和 select 我們的數據,使用Enumerable.Range作為“循環”構造:

List<Customer> customers = Enumerable.Range(0, 10).Select(c =>
    new Customer
    {
        Name = "Pedro",
        Age = "20",
        Products = Enumerable.Range(0, 3).Select(p => new Product
        {
            Code = "1",
            Price = "400.00"
        }).ToArray()
    }).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM