0

我不确定为什么会收到错误消息:“必须是可还原节点”

我尝试了此解决方案,但出现“必须是可还原节点”错误。我正在使用 EF Core 2.2 运行 Core 2

                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)
                {
                    return new BaseResponse<object>(ex.Message);
                }

我使用的数据库提供程序是 Pomelo.EntityFrameworkCore.MySql 版本 2.2.6

4

1 回答 1

0

您的GroupyBy()表达可能无法翻译。不太可能工作的行是以下行:

D = x.Closing.Value.TimeOfDay >= start ? x.Closing.Value.Day : x.Closing.Value.Day - 1

这里的问题是,SQL 不允许在GROUP BY子句中使用条件逻辑。

您可以将 select 子句中的条件逻辑移到 group by 子句之前,然后对其结果进行分组:

var totals = await _unitOfWork.Additions.GetAll()
   .Where(x => x.FirmId == firm.FirmId &&
               x.State == false &&
               x.Closing >= opening &&
               x.Closing <= closing)
   .Select(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
   })
   .GroupBy(x => new {
       Y = x.Y,
       M = x.M,
       D = x.D
   })
// ...

这通常会起作用,但在 Pomelo2.2.6中,没有翻译DateTime.TimeOfDay(虽然它支持3.0.0),所以您需要将日期的Hour,MinuteSecond值与TimeSpanstart变量)的相应属性进行比较。

于 2019-11-30T02:16:56.440 回答