ASP.NET MVC查询未显示不同值

时间:2019-03-15 10:10:50

标签: javascript c# asp.net-mvc entity-framework google-visualization

我正在使用以下查询在Google图表中使用。它运作良好,但没有显示明显的月份。有没有一种方法可以对每个HoursTaken的所有MonthOfHoliday求和,以避免没有不同的值?

 var querythpmpro = (from r in db.HolidayRequestForms
                     where r.EmployeeID == id
                     select new { Count = r.HoursTaken, Value = r.MonthOfHoliday.ToString() }).OrderBy(e => e.Value);

控制台Uncaught (in promise) Error: Type mismatch. Value 1 does not match type string in column index 0上的错误消息

编辑:

#region Total Hours Per Month

        var querythpmpro = (from r in db.HolidayRequestForms
                            where r.EmployeeID == id
                            group r by r.MonthOfHoliday into g
                            select new { Value = g.Key, Sum = g.Sum(h => h.HoursTaken) }
               ).OrderBy(e => e.Value);


        var resultthpmpro = querythpmpro.ToList();

        var datachartthpmpro = new object[resultthpmpro.Count];
        int Q = 0;
        foreach (var i in resultthpmpro)
        {
            datachartthpmpro[Q] = new object[] { i.Value, i.Sum};
            Q++;
        }
        string datathpmpro = JsonConvert.SerializeObject(datachartthpmpro, Formatting.None);
        ViewBag.datajthpmpro = new HtmlString(datathpmpro);

        #endregion

型号:

   public partial class HolidayRequestForm
  {
     public int RequestID { get; set; }
     public int EmployeeID { get; set; }
     public System.DateTime StartDate { get; set; }
     public System.DateTime FinishDate { get; set; }
     public decimal HoursTaken { get; set; }
     public string Comments { get; set; }
     public int YearCreated { get; set; }
     public int MonthCreated { get; set; }
     public int DayCreated { get; set; }
     public Nullable<int> YearOfHoliday { get; set; }
     public Nullable<bool> Approved { get; set; }
     public string SubmittedBy { get; set; }
     public string ApprovedBy { get; set; }
     public Nullable<int> WorkWeek { get; set; }
     public Nullable<int> MonthOfHoliday { get; set; }

     public virtual Employee Employee { get; set; }
  }

脚本:

  function drawChartA() {

    // Create the data table.

    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Value');
    data.addColumn('number', 'Count');
    data.addRows(datassthpmpro);

    // Set chart options
    var options = {
        'title': 'Holiday Hours Taken Per Month',
        'width': 600,
        'height': 350,
        'hAxis': { title: 'Month Number' },
        'vAxis': { title: 'Holiday Hours Taken' },
        'is3D': true,
        'legend': 'none'
    };

1 个答案:

答案 0 :(得分:0)

如何按MonthOfHoliday分组并在Sum上进行HoursTaken

var querythpmpro = (from r in db.HolidayRequestForms
                     where r.EmployeeID == id
                     group r by r.MonthOfHoliday into g
                     select new { Value =  g.Key, Sum = g.Sum(h => h.HoursTaken) }
                   ).OrderBy(e => e.Value);