计算Group Linq to Entity中的DateTime差值和

时间:2017-12-11 23:41:35

标签: c# linq linq-to-entities

我有一个单元测试没有返回任何东西,我很好奇,如果我的组或方法中的计算导致列表保持0计数。任何帮助或指向正确的方向将不胜感激。谢谢!

单元测试:

            #region CalculateTimeToPay_IntegerEmployeeIDAndDateTimeWeekOfDate_IQueryableTimeToPay
    [TestMethod]
    public void CalculateTimeToPay_IntegerEmployeeIDAndDateTimeWeekOfDate_IQueryableTimeToPay()
    {
        //Arrange
        var service = new WWIncomeTaxDataHandlerService("ProdSQL");
        DateTime payrollWeekEnd = Convert.ToDateTime("2017-01-22");
        DateTime payrollWeekStart = Convert.ToDateTime("2017-01-16");

        var V_Times = new List<V_Time>()
        {
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-05 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-05 16:00:00.000"), EmployeeID = 999 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-05 08:30:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-05 16:00:00.000"), EmployeeID = 777 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-05 08:45:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-05 16:00:00.000"), EmployeeID = 888 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-10 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-10 16:00:00.000"), EmployeeID = 999 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-10 08:30:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-10 16:00:00.000"), EmployeeID = 777 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-10 08:45:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-10 16:00:00.000"), EmployeeID = 888 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-11 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-11 16:00:00.000"), EmployeeID = 999 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-11 00:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-11 07:00:00.000"), EmployeeID = 777 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-11 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-11 16:00:00.000"), EmployeeID = 888 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-12 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-12 16:00:00.000"), EmployeeID = 999 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-12 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-12 16:44:00.000"), EmployeeID = 777 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-12 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-12 16:00:00.000"), EmployeeID = 888 },
            new V_Time { PayTimeIn = Convert.ToDateTime("2017-01-14 08:00:00.000"), PayTimeOut = Convert.ToDateTime("2017-01-14 16:07:00.000"), EmployeeID = 777 }
        };

        var mockRepository = new Mock<IRepository>();
        mockRepository.Setup(x => x.Find<V_Time>()).Returns(V_Times.AsQueryable());
        var builder = BuildContainer();
        builder.Register(x => mockRepository.Object).As<IRepository>();
        var container = builder.Build();

        var itrs = container.Resolve<WWIncomeTaxDataHandler.Domain.WWIncomeTaxDataHandlerService>();

        var expected = new List<calculatedHours>()
        {
            new calculatedHours { EmployeeID = 999, TimeToPay = 24 },
            new calculatedHours { EmployeeID = 888, TimeToPay = 31.35},
            new calculatedHours { EmployeeID = 777, TimeToPay = 23.25}
        };

        //Act
        var actual = itrs.CalculateTimeToPay(payrollWeekStart, payrollWeekEnd);

        //Assert
        var compareLogic = new CompareLogic();
        var result = compareLogic.Compare(actual, expected);
        Assert.IsTrue(result.AreEqual, result.DifferencesString);
    }
    #endregion

方法:

            #region CalculateTimeToPay(payrollWeekEnd)
    public IQueryable CalculateTimeToPay(DateTime payrollWeekStart, DateTime payrollWeekEnd)
    {
        var uow = container.Resolve<WWIncomeTaxDataHandlerUnitOfWork>();

        var employeeHours = (from time in uow.Accounting.Repository.Find<V_Time>()
                             where (payrollWeekEnd >= time.PayTimeIn && time.PayTimeIn >= payrollWeekStart)
                             group time by new { time.EmployeeID } into empGroup
                             select new calculatedHours
                             {
                                 EmployeeID = empGroup.Key.EmployeeID,
                                 TimeToPay = empGroup.Where(x => x.EmployeeID == empGroup.Key.EmployeeID).Sum (x => ((x.PayTimeOut - x.PayTimeIn).TotalSeconds) / 60 / 60)
                             }).ToList(); 


        return employeeHours.AsQueryable();
    }
    #endregion

请注意,tolist和返回查询是为了加快测试速度

1 个答案:

答案 0 :(得分:0)

你使用一堆匿名数据类型非常复杂。你可以尝试一下C#风格:

public class CalculatedHours
{
    public int EmployeeId { get; set; }
    public TimeSpan TimeToPay { get; set; }
}

public class PayTime
{
    public DateTime PayTimeIn { get; set; }
    public DateTime PayTimeOut { get; set; }
    public int EmployeeId { get; set; }

    public TimeSpan CalculateTimeToPay(DateTime payrollWeekStart, DateTime payrollWeekEnd)
    {
        DateTime realIn = payrollWeekStart > this.PayTimeIn ? payrollWeekStart : this.PayTimeIn;
        DateTime realOut = payrollWeekEnd < this.PayTimeOut ? payrollWeekEnd : this.PayTimeOut;

        return realOut - realIn;
    }

    public static void Test()
    {
        List<PayTime> someData = new List<PayTime>() { };

        DateTime payrollWeekEnd = Convert.ToDateTime("2017-01-22");
        DateTime payrollWeekStart = Convert.ToDateTime("2017-01-16");

        IEnumerable<CalculatedHours> result = someData.GroupBy(g => g.EmployeeId)
            .Select(s => new CalculatedHours() { EmployeeId = s.Key, TimeToPay = new TimeSpan(s.Sum(a => a.CalculateTimeToPay(payrollWeekStart, payrollWeekEnd).Ticks)) });
       // Do some assertions
    }
}

顺便说一下。你的Testdata错了。 payrollWeekStartpayrollWeekEnd为时已晚(2017-01-16至2017-01-22数据最高为2017-01-14)。 另外,你不计算兼职支付..