简体   繁体   English

按日期分组的实体框架总和时间

[英]Entity Framework sum time grouped by a date

I have the following code and I get the error我有以下代码,我得到了错误

Cannot implicitly convert type 'string' to 'long'无法将类型“字符串”隐式转换为“长”

The error is coming from (i => i.TotalHours) .错误来自(i => i.TotalHours)

What am I doing wrong?我究竟做错了什么? Or what do I have to change?或者我需要改变什么?

Table桌子

实体框架表

ViewModel视图模型

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: 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; }
}

According to your error I believe you need either to change your TotalHours type to long or to make a type conversion, try: (long)Convert.ToDouble(i.TotalHours)根据您的错误,我相信您需要将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM