Asp.net mvc 如何使用mvc图表助手计算工作小时数和格式化日期,使之仅显示mvc中的月份

Asp.net mvc 如何使用mvc图表助手计算工作小时数和格式化日期,使之仅显示mvc中的月份,asp.net-mvc,charts,asp.net-mvc-5,asp.net-mvc-helpers,Asp.net Mvc,Charts,Asp.net Mvc 5,Asp.net Mvc Helpers,这是我目前正在使用的代码,我想知道在使用mvc图表时这两件事是否可行?以及如何着手去做 我的控制器 public ActionResult CharterColumn() { ArrayList xValue = new ArrayList(); ArrayList yValue = new ArrayList(); var results = (from c in db.Timesheets select c); results.ToList().ForEa

这是我目前正在使用的代码,我想知道在使用mvc图表时这两件事是否可行?以及如何着手去做

我的控制器

public ActionResult CharterColumn()
{
    ArrayList xValue = new ArrayList();
    ArrayList yValue = new ArrayList();

    var results = (from c in db.Timesheets select c);

    results.ToList().ForEach(rs => xValue.Add(rs.Date));
    //I want to format this to show the only the months on the x axis

    results.ToList().ForEach(rs => yValue.Add(rs.Hours));
    //And I want to calculate the sum of the hours for each month worked by a specific employee

    new Chart(width: 800, height: 400, theme: ChartTheme.Yellow)
    .AddTitle("Test")
    .AddSeries("Default", chartType: "Column", xValue: xValue, yValues: yValue)
    .Write("bmp");

    return null;
}
我的看法呢

<div>
   <img src= "@Url.Action("CharterColumn")" alt="Chart"/>
</div>
您可以按月份和年份对时间表记录进行分组,如下所示:

DateTimeFormatInfo dtfi = new DateTimeFormatInfo();

// gets the records grouped based on Year and Month. 
// Creates an object of IEnumerable<IGrouping<>> type
var groupedByMonth = result
                        .OrderByDescending(x => x.Date)
                        .GroupBy(x => new { x.Date.Year, x.Date.Month }).ToList();


// gets the months names in a list
List<string> monthNames = groupedByMonth
                    .Select(a => dtfi.GetAbbreviatedMonthName(a.Key.Month))
                    .ToList();

// gets the total hours per month
List<int> hoursPerMonth = groupedByMonth
                        .Select(a => a.Sum(p => p.Hours))
                        .ToList();


ArrayList xValue = new ArrayList(monthNames);
ArrayList yValue = new ArrayList(hoursPerMonth);
我用这个月的名字。您也可以通过以下方式实现此目的:

List<string> monthNames = groupedByMonth
                    .Select(a => a.FirstOrDefault().Date.ToString("MMMM"))
                    .ToList();

EntityFramework.SqlServer.dll中发生类型为“System.NotSupportedException”的异常,但未在用户代码中处理其他信息:LINQ to Entities无法识别方法“System.String Get缩写月名Int32”方法,并且无法将此方法转换为存储表达式。嗨,我试过了,我明白了error@Kimberly尝试在没有dtfi的情况下执行此操作,如最后所述。我认为您必须在linq查询之前执行.ToList。附加信息:linq to Entities无法识别方法“System.String ToString ToString System.String”,无法将此方法转换为存储表达式。仍在获取this@Kimberly在此行的结果之后添加一个ToList:var groupedByMonth=result.ToList.OrderByDescendingx=>x.Date