![](/img/trans.png)
[英]How to use Entity Framework group date not date with time and sum total where condition
[英]Entity Framework sum time grouped by a date
我有以下代碼,我得到了錯誤
無法將類型“字符串”隱式轉換為“長”
錯誤來自(i => i.TotalHours)
。
我究竟做錯了什么? 或者我需要改變什么?
桌子
視圖模型
private void CalculateTime()
{
var result = context.TimeSheets.GroupBy(o => o.StartDate)
.Select(g => new { StartDate = g.Key, total = g.Sum(i => i.TotalHours) });
foreach (var group in result)
{
Console.WriteLine("StartDate = {0} TotalHours={1}", group.StartDate, group.total);
}
}
Class:
public class TimeSheet
{
[Key]
public int ID { get; set; }
[StringLength(255)]
public string MachineID { get; set; }
public DateTime StartDate { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndDate { get; set; }
public DateTime EndTime { get; set; }
[StringLength(225)]
public string WorkDescription { get; set; }
[StringLength(225)]
public string ClientName { get; set; }
[StringLength(225)]
public string UserName { get; set; }
public string TotalHours { get; set; }
}
根據您的錯誤,我相信您需要將TotalHours
類型更改為long
或進行類型轉換,請嘗試: (long)Convert.ToDouble(i.TotalHours)
var result = context.TimeSheets.GroupBy(o => o.StartDate)
.Select(g => new { StartDate = g.Key, total = g.Sum(i => (long)Convert.ToDouble(i.TotalHours)) });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.