2017-09-16 38 views
0

这是我正在工作的代码我想知道当使用mvc图表时这两种情况是否可能?而如何去这样做他们如何计算工作时间的总和和日期格式,只显示mvc使用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> 

回答

1

你可以把这些TimeSheet记录的月和年这样的:

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); 

我正在使用DateTimeFormatInfo来获取月份的名称。您也可以做到这一点没有这个:

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

“System.NotSupportedException”类型的异常出现在EntityFramework.SqlServer.dll但在用户代码 其他信息没有处理:LINQ到实体无法识别方法'System.String GetAbbreviatedMonthName(Int32)'方法,并且此方法不能转换为存储表达式。嗨,我已经试过它得到这个错误 – Kimberly

+0

@Kimberly尝试没有'dtfi',如最后所述。我认为你必须在linq查询之前执行'.ToList()'。 – adiga

+0

附加信息:LINQ to Entities不识别方法'System.String ToString(System.String)'方法,并且此方法不能转换为存储表达式。 – Kimberly

相关问题