从 json 视图转换数据集 .Net Web API

convert dataset from json view .Net Web API

我正在尝试在我的 angular 应用程序中创建图表。为此,我想在我的 .Net Web API 中按如下方式传递数据集。

但在我的代码中它传递的数据集如下

[{"Column1":1,"_Month":"January","SumOfMonth":-14900.40,"NoOfOutlets":11},{"Column1":2,"_Month":"February","SumOfMonth":-17856.00,"NoOfOutlets":2},{"Column1":3,"_Month":"March","SumOfMonth":-5312.00,"NoOfOutlets":4},{"Column1":4,"_Month":"April","SumOfMonth":-22103.13,"NoOfOutlets":15},{"Column1":6,"_Month":"June","SumOfMonth":-16014.72,"NoOfOutlets":5},{"Column1":7,"_Month":"July","SumOfMonth":-63251.93,"NoOfOutlets":46}

现在我想将我的数据集转换为上述格式来创建图表。

我的控制器

public class MonthSelloutController : ControllerBase
    {
        private readonly VantageContext _context;
        public MonthSelloutController(VantageContext context)
        {
            _context = context;
        }
        //GET: api/AllSellOut
        [HttpGet]
        public ActionResult<string> Getset(string Year)
        {
            DataTable dt = new MonthSelloutMgt().Monthsellout(Year, _context);
            string json = JsonConvert.SerializeObject(dt);
            return json;
        }
    }

我的BL

public DataTable Monthsellout(string Year, VantageContext _context)
        {
            DataTable dt = new DataTable();
            try
            {
                string conn = _context.Database.GetDbConnection().ConnectionString;
                using (SqlConnection con = new SqlConnection(conn))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("D_MonthSellOut", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Year", Year));

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return dt;
        }

我可以做什么来转换我在上图中提到的数据集。非常感谢任何帮助。

我的存储过程的结果

因为您想为您的 JSON 字符串获取自定义架构,您可以尝试编写一个 class 来携带来自 DataTable 的数据,如下所示

public class ViewModel
{
    public List<List<object>> SumOfMonth { get; set; } = new List<List<object>>();
    public List<List<object>> NoOutlets { get; set; } = new List<List<object>>();
}

然后我们可以使用DataTable中的foreach迭代器数据填充数据到ViewModel

[HttpGet]
public ActionResult<string> Getset(string Year)
{
    DataTable dt = new MonthSelloutMgt().Monthsellout(Year, _context);
    ViewModel result = new ViewModel();
    
        foreach (DataRow item in dt.Rows)
        {
            result.SumOfMonth.Add(new List<object>(){
            (decimal)item["SumOfMonth"],
            item["_Month"].ToString()
            });
        }
        foreach (DataRow item in dt.Rows)
        {
            result.NoOfOutlets.Add(new List<object>(){
            (int)item["NoOfOutlets"],
            item["_Month"].ToString()
            });
        }        string json = JsonConvert.SerializeObject(result);
    return json;
}

我会使用 Sqldatareader 而不是 DataTable 来读取数据结果,因为 Sqldatareader 可能会提供比 DataTable

更高的性能