创建LINQ查询对象

时间:2018-05-18 18:10:16

标签: c# json linq

我试图在y轴Canvas JS图上绘制三组不同的数据点(GraphPoints,GraphPoints1,GraphPoints2)但是当我在Controller代码上添加所有这些集时,结果不会被绘制。如果我只添加一组(例如y = s.GraphPoints1),则生成图形并绘制结果。但是,我想在一个图上的y轴上的所有数据点集(GraphPoints,GraphPoints1,GraphPoints2)。我在将数据点集(GraphPoints,GraphPoints1,GraphPoints2)置于y下时遇到了麻烦。请有人帮忙,让我知道我做错了什么。谢谢!

这有效:

 public ActionResult Graph(int machineId)
{
    using (var db = new DatabaseModel())
    {
        var sheets = db.Checksheets
            .Where(s => s.MachineId == machineId)
            .ToList()
            .Select(s => new
            {
                label = $"O: {s.OrderNum} P: {s.PartNum}",
                y = s.GraphPoints                   
            });

        return Json(sheets, JsonRequestBehavior.AllowGet);
    }
}

这不起作用,但我希望在一个图表上显示所有这些数据点集:

 public ActionResult Graph(int machineId)
{
    using (var db = new DatabaseModel())
    {
        var sheets = db.Checksheets
            .Where(s => s.MachineId == machineId)
            .ToList()
            .Select(s => new
            {
                label = $"O: {s.OrderNum} P: {s.PartNum}",
                y = $"{s.GraphPoints} {s.GraphPoints1} {s.GraphPoints2}"
            });

        return Json(sheets, JsonRequestBehavior.AllowGet);
    }
}

这是记录图形点的SQL表:

 GraphPoints           GraphPoints1        GraphPoints2
    50                     45                   60
    30                     12                   100
    56                     89                   67

1 个答案:

答案 0 :(得分:0)

Concat方法可用于将所有GraphPoints置于y。

之下
  public ActionResult Graph(int machineId)
  {
using (var db = new DatabaseModel())
{
    var sheets = db.Checksheets
        .Where(s => s.MachineId == machineId)
        .ToList()
        .Select(s => new
        {
            label = $"O: {s.OrderNum} P: {s.PartNum}",
            y = s.GraphPoints
        });
         var sheets1 = db.Checksheets
        .Where(s => s.MachineId == machineId)
        .ToList()
        .Select(s => new
        {
            label = $"O: {s.OrderNum} P: {s.PartNum}",
            y = s.GraphPoints1
        });
         var sheets2 = db.Checksheets
        .Where(s => s.MachineId == machineId)
        .ToList()
        .Select(s => new
        {
            label = $"O: {s.OrderNum} P: {s.PartNum}",
            y = s.GraphPoints2
        });

    return Json(sheets.Concat(sheets1).Concat(sheets2), JsonRequestBehavior.AllowGet);
     }
   }