【问题标题】:Linq group by year, month and employeeid then sum total working hoursLinq 按年、月和员工 ID 分组,然后总工作时间相加
【发布时间】:2015-10-23 03:58:17
【问题描述】:

我正在使用此代码按年、月和employeeId 对行进行分组。

var dtrSummary = from dtr in db.DTRs
                 group dtr by new { dtr.Date.Year, dtr.Date.Month, dtr.EmployeeId } into g
                 select new
                 {
                     Year = g.Key.Year,
                     Month = g.Key.Month,
                     NoOfDays = g.Count(),
                     FullName = db.EmployeeRates.Where(e => e.Id == g.Key.EmployeeId).Select(e => e.FullName).FirstOrDefault(),

                     Time1 = // Get total hours between two datetime
                             // DateTime timein = new DateTime(year, month, day, tihh1, timm1, 0);
                             // DateTime timeout = new DateTime(year, month, day, tohh1, tomm1, 0);
                             // Timespan totalTime1 = timeout - timein;
                 };

但我不知道如何使用 linq group by 来获取总小时数,就像您在上面看到的那样。

这是我的桌子:

+----------------------------------------------------------------------+
|    Date   | Timeinhh | Timeinmm | Timeouthh | Timeoutmm | EmployeeId |
+----------------------------------------------------------------------+
| 10/1/2015 | 9        | 0        | 18        | 0         | 1          |
| 10/2/2015 | 9        | 0        | 18        | 0         | 2          |
| 10/3/2015 | 9        | 0        | 18        | 0         | 1          |
| 10/4/2015 | 9        | 0        | 18        | 0         | 2          |
+----------------------------------------------------------------------+

【问题讨论】:

    标签: linq


    【解决方案1】:

    您可能希望在分组 DTR 之前计算每个小时。

    var DTRs2 = db.DTRs.ToList().Select(d => new
    {
        Date = d.Date,
        Hours = new DateTime(d.Date.Year, d.Date.Month, d.Date.Day, d.Timeouthh, d.Timeoutmm, 0)
                    .Subtract(new DateTime(d.Date.Year, d.Date.Month, d.Date.Day, d.Timeinhh, d.Timeinmm, 0)).TotalHours,
        EmployeeId = d.EmployeeId
    }).ToList();
    
    var dtrSummary =
        (from dtr in DTRs2
         group dtr by new { dtr.Date.Year, dtr.Date.Month, dtr.EmployeeId } into g
         select new
         {
             Year = g.Key.Year,
             Month = g.Key.Month,
             NoOfDays = g.Count(),
             EmpId = g.Key.EmployeeId,
             FullName = db.EmployeeRates.Where(e => e.Id == g.Key.EmployeeId).Select(e => e.FullName).FirstOrDefault(),
    
             Time1 = g.Sum(dtr => dtr.Hours) // Get total hours between two datetime
         });
    

    我还建议像这样定义另一个类/表,而不是每次都使用匿名类型:

    public class DTRDetails
    {
        public DTR DTR { get; set; }
        public EmployeeRate EmployeeRate { get; set; }
    
        public string FullName
        {
            get { return EmployeeRate.FullName; }
        }
    
        public int Hours
        {
            get
            {
                return (int)(new DateTime(DTR.Date.Year, DTR.Date.Month, DTR.Date.Day, DTR.Timeinhh, DTR.Timeinmm, 0)
                                 .Subtract(new DateTime(DTR.Date.Year, DTR.Date.Month, DTR.Date.Day, DTR.Timeouthh, DTR.Timeoutmm, 0))).TotalHours;
            }
        }
    
        public int Hours2
        {
            get
            {
                // return something
            }
        }
    
        public int Hours3
        {
            get { return Hours - Hours2; }
        }
    }
    

    【讨论】:

    • 嗨@jhmt 谢谢你的回答我认为这是我的问题的答案,但我收到此错误“LINQ to Entities 无法识别方法'System.TimeSpan Subtract(System. DateTime)' 方法,并且该方法不能翻译成存储表达式。"
    • 嗨@JohnLouieDelaCruz 我已经更新了我的答案。如果您使用 ToList 这样的方法生成新列表,这将起作用。
    • 不客气@JohnLouieDelaCruz 对了,你也可以试试System.Data.Objects.EntityFucntions.DiffHours:)
    • 嗨@jhmt 我还有一个问题,例如如何获得多个时间跨度的总和,我将添加新属性,例如 TotalHours = Hours + Hours2
    • 嗨@JohnLouieDelaCruz 我已经更新了我的答案。为了做到这一点,我建议使用我更新的答案中的新类或表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多