Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架核心组(按日期范围)_Entity Framework_.net Core - Fatal编程技术网

Entity framework 实体框架核心组(按日期范围)

Entity framework 实体框架核心组(按日期范围),entity-framework,.net-core,Entity Framework,.net Core,我试图根据天数来计算总数,但有时天数可能会延长。例如,一天的时间段介于00:00和23:59之间。当您根据这个时间段生成以下代码时,这无关紧要,它给出了正确的总数 opening = opening.AddDays(-6); var totals = await _unitOfWork.Additions.GetAll().Where(x => x.FirmId == firm.FirmId && x.Sta

我试图根据天数来计算总数,但有时天数可能会延长。例如,一天的时间段介于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();
opening=opening.AddDays(-6);
var totals=await\u unitOfWork.Additions.GetAll()。其中(x=>x.FirmId==firm.FirmId&&x.State==false&&x.Closing>=期初和期末新{x.Closing.Value.Year,x.Closing.Value.Month,x.Closing.Value.Day})
。选择(s=>new
{
onKey=Convert.ToDateTime(新的日期时间(s.Key.Year、s.Key.Month、s.Key.Day)).ToSortDateString(),
总计=s.Sum(c=>c.Price)
}).ToListAsync();
但是,营业时间和营业时间可能因企业而异,因此每天的总数不能给出每个企业的正确结果

例如,如果一家公司的营业时间在早上07:00到晚上02:00之间,则上述代码块将无法正常工作。因为,假设最后一个关闭的帐户是夜间01:00,上面的代码块会将此关闭的帐户分配到另一天,因为关闭的帐户在新的一天关闭

但我想要的是,我需要早上营业时间和晚上营业时间的总和

例如,-->

公司1开始时间:上午7:00结束时间:下午23:59时间范围输出-->*2019年11月18日总计: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");
                }
试试看
{
TimeSpan start=new TimeSpan(Convert.ToInt32(firm.OpeningHours.Split('-')[0]。Split(':')[0]),Convert.ToInt32(firm.OpeningHours.Split('-')[0]。Split(':')[1]),0);
var totals=await\u unitOfWork.Additions.GetAll(),其中(x=>x.FirmId==firm.FirmId&&x.State==false&&x.Closing>=期初和期初结束
新的{
Y=x.期末价值.年度,
M=x.收盘价.月份,
D=x.Closing.Value.TimeOfDay>=开始?x.Closing.Value.Day:x.Closing.Value.Day-1
})
。选择(s=>new
{
onKey=Convert.ToDateTime(新的日期时间(s.Key.Y,s.Key.M,s.Key.D)).ToSortDateString(),
总计=s.Sum(c=>c.Price)
}).ToListAsync();
返回新的BaseResponse(总计);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
返回新的BaseResponse(“null”);
}

我使用的DB提供程序是Pomelo.EntityFrameworkCore.MySql

如果您通过表达式调整组,应该能够获得准确的结果。每项新增项目的有效截止日期取决于确定的营业时间。像这样的方法应该会奏效:

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();
var总计=等待\u工作单元
.补充
.GetAll()
.其中(x=>
x、 FirmId==firm.FirmId&&
x、 State==false&&
x、 关闭>=打开和关闭
x、 结束
新{
x、 收盘价,年,
x、 月收盘价,
(x.Closing.Value.TimeOfDay>=公司开盘时间)?x.Closing.Value.Day:x.Closing.Value.Day-1
})          
。选择(s=>new
{
onKey=Convert.ToDateTime(新的日期时间(s.Key.Year、s.Key.Month、s.Key.Day)).ToSortDateString(),
总计=s.Sum(c=>c.Price)
}).ToListAsync();

如果您通过表达式调整组,您应该能够获得准确的结果。每项新增项目的有效截止日期取决于确定的营业时间。像这样的方法应该会奏效:

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();
var总计=等待\u工作单元
.补充
.GetAll()
.其中(x=>
x、 FirmId==firm.FirmId&&
x、 State==false&&
x、 关闭>=打开和关闭
x、 结束
新{
x、 收盘价,年,
x、 月收盘价,
(x.Closing.Value.TimeOfDay>=公司开盘时间)?x.Closing.Value.Day:x.Closing.Value.Day-1
})          
。选择(s=>new
{
onKey=Convert.ToDateTime(新的日期时间(s.Key.Year、s.Key.Month、s.Key.Day)).ToSortDateString(),
总计=s.Sum(c=>c.Price)
}).ToListAsync();

因此,您将无法按年、月、日进行分组,因为这些不适用于“工作日”。那些是日历日。你需要定义你的工作日,然后根据它分组。@devl那么,我如何逻辑地做到这一点呢?这些营业时间是否存储在
公司
中?@GertArnold是的,我存储在公司实体中。因此,你将无法按年、月、日分组,因为这些不适用于“工作日”。那些是日历日。你需要定义你的工作日,然后根据它分组。@devl那么,我如何逻辑地做到这一点?这些营业时间是否存储在
公司中?@GertArnold是的,我存储在公司实体中应该存储在公司中的内容。Openinghours?,my Firm.Openinghours实体存储字符串值-->>例如:“07:00-02:00”第一时间是存储在P.M ValueDay=(x.Closing.Value.TimeOfDay>=TimeSpan.Parse(firm.OpeningHours.ToString())中的.M秒时间?x、 Closing.Value.Day:x.Closing.Value.Day-1-----不起作用我忘了说,我使用的是pomero.EntityFrameworkCore.MySqlI假定为firm.OpeningHours将有一个timespan值,而不是字符串。fir中应该存储什么