2

I want to be able to mock my average date diff calculation in my LINQ.

My Code:

IDictionary<int, TimeSpan> calculations = (
            from log in repository.Get<WorkLog>()
            join work in repository.Get<Work>() on log.WorkId equals work.Id
            group log by log.WorkId into step
            select new
            {
                Id = step.Key,
                AverageTime = step.Average(x =>DbFunctions.DiffSeconds(x.StartDate, x.EndDate))
            }
        ).ToDictionary(x => x.Id, y => TimeSpan.FromSeconds(y.AverageTime.HasValue ? y.AverageTime.Value : 0.0));

Since the repository doesn't always contain a DbContext, for example when I am running tests. I have tried using

AverageTime = step.Average(x => (x.EndDate - x.StartDate).TotalSeconds))

But that is not supported by Entity Framework.

I want to find a way that I can perform this action independent of a DbContext or not. Any suggestions?

Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34

0 Answers0