如何以数组格式更改C#模型列表?

时间:2017-07-24 08:46:54

标签: c# listview arraylist

我正在尝试将List输出更改为Array,因为它更容易读取json输出。

这是当前的输出:

{
  "businessvalue": [
    {
      "BVSId": 1,
      "BVSROI": [
        {
          "BVSROIId": 1,
          "ROIItem": "Subscription Costs",
          "ROIYear": "Initial",
          "ROIYearValue": 10,
          "BVSId": 1,
          "BusinessValue": null
        },
        {
          "BVSROIId": 2,
          "ROIItem": "Subscription Costs",
          "ROIYear": "1",
          "ROIYearValue": 20,
          "BVSId": 1,
          "BusinessValue": null
        },
        {
          "BVSROIId": 3,
          "ROIItem": "Subscription Costs",
          "ROIYear": "2",
          "ROIYearValue": 30,
          "BVSId": 1,
          "BusinessValue": null
        },
        {
          "BVSROIId": 4,
          "ROIItem": "Subscription Costs",
          "ROIYear": "3",
          "ROIYearValue": 40,
          "BVSId": 1,
          "BusinessValue": null
        },
        {
          "BVSROIId": 5,
          "ROIItem": "Subscription Costs",
          "ROIYear": "4",
          "ROIYearValue": 50,
          "BVSId": 1,
          "BusinessValue": null
        }
      ]
    }
  ]
}

这是我需要的预期输出:

    {
      "businessvalue": [
        {
          "BVSId": 1,
          "BVSROI": [
            {
              "BVSROIId": 1,
              "ROIItem": "Subscription Costs",
              "UserInput": [
                {
                  "Initial": 0,
                  "Year1": 10,
                  "Year2": 20,
                  "Year3": 30,


            "Year4": 40
            }
          ],
          "BVSId": 1,
          "BusinessValue": null
        }
      ]
    }
  ]
}

目前我的模型是这样完成的:

namespace WebService.Models
{
    public class BusinessValue
    {
        [Key]
        public int BVSId { get; set; }

        public List<BVSROI> BVSROI { get; set; }
    }

    public class BVSROI
    {
        [Key]
        public int BVSROIId { get; set; }
        [Required]
        public string ROIItem { get; set; }
        public string ROIYear { get; set; }
        public double ROIYearValue { get; set; }

        // Foreign Key
        public int BVSId { get; set; }
        // Navigation property
        public BusinessValue BusinessValue { get; set; }

    }
}

我是c#的新手,我希望你可以指导/帮助我。

====

@Rostech,

我创建了一个单独的表

但这就是我所生成的:

{
  "BusinessValue": [
    {
      "BVSId": 1,
      "Year1": 10,
      "Year2": 20,
      "Year3": 30,
      "CLB": 1,
      "BVSROI": [
        {
          "BVSROIId": 1,
          "ROIItem": "Subscription Costs",
          "UserInput": [
            {
              "BVSROIUIId": 1,
              "ROIYear": "Initial",
              "ROIYearValue": 10,
              "BVSROI": null
            },
            {
              "BVSROIUIId": 2,
              "ROIYear": "1",
              "ROIYearValue": 20,
              "BVSROI": null
            },
            {
              "BVSROIUIId": 3,
              "ROIYear": "2",
              "ROIYearValue": 30,
              "BVSROI": null
            },
            {
              "BVSROIUIId": 4,
              "ROIYear": "3",
              "ROIYearValue": 40,
              "BVSROI": null
            },
            {
              "BVSROIUIId": 5,
              "ROIYear": "4",
              "ROIYearValue": 50,
              "BVSROI": null
            }
          ]
        }
      ]
    }
  ]
}

,我期待的输出是这样的:

 {
      "businessvalue": [
        {
          "BVSId": 1,
          "BVSROI": [
            {
              "BVSROIId": 1,
              "ROIItem": "Subscription Costs",
              "UserInput": [
                {
                  "Initial": 0,
                  "Year1": 10,
                  "Year2": 20,
                  "Year3": 30,


            "Year4": 40
            }
          ],
          "BVSId": 1,
          "BusinessValue": null
        }
      ]
    }
  ]
}

可以通过这种方式完成模型:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WebService.Models
{
    public class BusinessValue
    {
        [Key]
        public int BVSId { get; set; }
        [Required]
        public int Year1 { get; set; }
        public int Year2 { get; set; }
        public int Year3 { get; set; }
        public int CLB { get; set; }
        public List<BVSROI> BVSROI { get; set; }
    }

    public class BVSROI
    {
        [Key]
        public int BVSROIId { get; set; }
        [Required]
        public string ROIItem { get; set; }
        public List<BVSROIUserInput> BVSROIUserInput { get; set; }

        // Foreign Key
        public int BVSId { get; set; }
        // Navigation property
        public BusinessValue BusinessValue { get; set; }

    }

    public class BVSROIUserInput
    {
        [Key]
        public int BVSROIUIId { get; set; }
        [Required]
        public string ROIYear { get; set; }
        public double ROIYearValue { get; set; }

        // Foreign Key
        public int BVSROIId { get; set; }
        // Navigation property
        public BVSROI BVSROI { get; set; }

    }

}

这就是我从控制器获取数据的方式:

var bvfsroiuis = (from users in db.BusinessValue ...
                  join bvfsroi in db.BVSROIs on bvfs.BVSId equals bvfsroi.BVSId
                  join bvfsroiui in db.BVSROIUserInputs on bvfsroi.BVSROIId equals bvfsroiui.BVSROIId
                  where u.UserId == id 
                   bvfsroiui
                  ).ToList();

List<BVSROIUserInput> bvfsroiui1 = new List<BVSROIUserInput>();
foreach (var b in bvfsroiuis)
                {

                       bvfsroiui1.Add(new BVSROIUserInput()
                    {
                        BVSROIUIId = b.BVSROIUIId,
                        ROIYear = b.ROIYear,
                        ROIYearValue = b.ROIYearValue

                    });
    }

我的数据库是这样完成的 http://imgur.com/a/AcJcn

我可以知道如何实现预期的产量?感谢帮助。

2 个答案:

答案 0 :(得分:1)

您必须更改BVSROI型号。

这是一些可能的编辑。但我不确定这是否有效,因为我不知道你的数据是如何获取的。

public class BVSROI
{
   [Key]
   public int BVSROIId { get; set; }
   [Required]
   public string ROIItem { get; set; }
   public string ROIYear { get; set; }
   public Dictionary<string, double> UserInput { get; set; }
   // Foreign Key
   public int BVSId { get; set; }
   // Navigation property
   public BusinessValue BusinessValue { get; set; }
}

答案 1 :(得分:1)

您好,您可以遵循以下课程结构。

public class BusinessValue
{
    [Key]
    public int BVSId { get; set; }

    public List<BVSROI> BVSROI { get; set; }
}

public class BVSROI
{
    [Key]
    public int BVSROIId { get; set; }
    [Required]
    public string ROIItem { get; set; }
    public UserInput UserInputs { get; set; }
    // Foreign Key
    public int BVSId { get; set; }
    // Navigation property
    public BusinessValue BusinessValue { get; set; }

}
public class UserInput
{
    public int Initial { get; set; 
    public int year1 { get; set; }
    public int year2 { get; set; }
    public int year3 { get; set; }
}