我尝试根据天数总计,但有时天数会延长。例如,一天的时间段在00:00到23:59之间。当您根据此时间范围编写以下代码时,没关系,它会给出正确的总数。
opening = opening.AddDays(-6);
var totals = await _unitOfWork.Additions.GetAll().Where(x => x.FirmId == firm.FirmId && x.State == false && x.Closing >= opening && x.Closing <= closing)
.GroupBy(x => new { x.Closing.Value.Year, x.Closing.Value.Month, x.Closing.Value.Day })
.Select(s => new
{
onKey = Convert.ToDateTime(new DateTime(s.Key.Year, s.Key.Month, s.Key.Day)).ToShortDateString(),
total = s.Sum(c => c.Price)
}).ToListAsync();
但是,营业时间和营业时间可能会因企业而异,因此每天的合计并不能给出每个企业的正确结果。
例如,如果一家公司的营业时间在早上07:00和晚上02:00之间,则上面的代码块将无法正常工作。因为,假设最后一个关闭的帐户是晚上01:00,那么上面的代码块会将这个关闭的帐户分配给另一天,因为关闭的帐户是在新的一天关闭的。
但是我想要的是我需要从早上营业到晚上营业的时间。
例如--->
公司1 开张:07:00 AM 关门:23:59 PM 时间范围 [07:00-23:59]输出-> * 18.11.2019 总计:1000 $
公司2 开业:上午07:00 关门:上午02:00(第二天,2019年11月19日)时间范围 [07: 00-02:00]输出-> 18.11.2019 总计:1200 $
我尝试了此解决方案,但出现了“必须是可还原节点”错误。
try
{
TimeSpan start = new TimeSpan(Convert.ToInt32(firm.OpeningHours.Split('-')[0].Split(':')[0]), Convert.ToInt32(firm.OpeningHours.Split('-')[0].Split(':')[1]), 0);
var totals = await _unitOfWork.Additions.GetAll().Where(x => x.FirmId == firm.FirmId && x.State == false && x.Closing >= opening && x.Closing <= closing)
.GroupBy(x =>
new {
Y = x.Closing.Value.Year,
M = x.Closing.Value.Month,
D = x.Closing.Value.TimeOfDay >= start ? x.Closing.Value.Day : x.Closing.Value.Day - 1
})
.Select(s => new
{
onKey = Convert.ToDateTime(new DateTime(s.Key.Y, s.Key.M, s.Key.D)).ToShortDateString(),
total = s.Sum(c => c.Price)
}).ToListAsync();
return new BaseResponse<object>(totals);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return new BaseResponse<object>("null");
}
我正在使用的数据库提供程序是Pomelo.EntityFrameworkCore.MySql
答案 0 :(得分:0)
如果您通过表达式调整组,则应该能够获得准确的结果。每次添加的有效截止日期取决于公司的营业时间。这样的事情应该起作用:
var totals = await _unitOfWork
.Additions
.GetAll()
.Where(x =>
x.FirmId == firm.FirmId &&
x.State == false &&
x.Closing >= opening &&
x.Closing <= closing)
.GroupBy(x =>
new {
x.Closing.Value.Year,
x.Closing.Value.Month,
(x.Closing.Value.TimeOfDay >= firm.OpeningHours)? x.Closing.Value.Day : x.Closing.Value.Day - 1
})
.Select(s => new
{
onKey = Convert.ToDateTime(new DateTime(s.Key.Year, s.Key.Month, s.Key.Day)).ToShortDateString(),
total = s.Sum(c => c.Price)
}).ToListAsync();