Entity Core 2.0.3 异常:必须是可简化节点
Entity Core 2.0.3 Exception: must be reducible node
我正在尝试将以下 sql 查询转换为 EF
sql 查询
Select Sum([KiloWatt]) as 'Sum',
Min([KiloWatt]) as 'Min',
Max([KiloWatt]) as 'Max',
Sum([KiloWatt])/COUNT([KiloWatt]) as 'Average',
CONVERT(date, [DateTime])
from OneHourElectricitys
where [DateTime] < SYSDATETIME()
group by CONVERT(date, [DateTime])
EF 代码
var analytics = await _analyticsRepository.Query().Where(x => x.DateTime.Subtract(DateTime.Today).Days < 0)
.GroupBy(x => x.DateTime.Date)
.Select(x => new
{ DateTime = x.Key,
Max = x.Max(y => y.KiloWatt),
Min = x.Min(y => y.KiloWatt),
Avg = x.Sum(y => y.KiloWatt)/ x.Count(),
Sum = x.Sum(y => y.KiloWatt)
})
.ToListAsync();
堆栈跟踪:-
System.ArgumentException: must be reducible node
at System.Linq.Expressions.Expression.ReduceAndCheck()
at System.Linq.Expressions.Expression.ReduceExtensions()
我是不是做错了?
-- 更新--
以下 2 个 EF 查询 运行 没问题,但合并它们会出现错误。
var analytics = await _analyticsRepository.Query().Where(x => x.DateTime.Subtract(DateTime.Today).Days < 0)
var analytics = await _analyticsRepository.Query().GroupBy(x => x.DateTime.Date)
.Select(x => new
{ DateTime = x.Key,
Max = x.Max(y => y.KiloWatt),
Min = x.Min(y => y.KiloWatt),
Avg = x.Sum(y => y.KiloWatt)/ x.Count(),
Sum = x.Sum(y => y.KiloWatt)
})
.ToListAsync();
这是我的解决方案并且有效。
var analytics = await (_analyticsRepository.Query()
.Where(x => x.PanelId.Equals(panelId, StringComparison.CurrentCultureIgnoreCase)
&& DateTime.Compare(x.DateTime, new DateTime()) < 0))
.GroupBy(x => x.DateTime.Date)
.Select(x => new
{
DateTime = x.Key,
Max = x.Max(y => y.KiloWatt),
Min = x.Min(y => y.KiloWatt),
Avg = x.Sum(y => y.KiloWatt) / x.Count(),
Sum = x.Sum(y => y.KiloWatt)
}).ToListAsync();
谢谢,
如 Ivan Stoev 所述,此问题的正确解决方案是升级到 EF Core 2.1.3
使用
DateTimeField.Year +""+ DateTimeField.Month //Use this
而不是 使用自定义日期格式,例如
DateTimeField.ToString("yyyy - MM") //Don't use this
为我解决了问题
顺便说一句,我的 .net 核心版本是 2。2.x
我正在尝试将以下 sql 查询转换为 EF
sql 查询
Select Sum([KiloWatt]) as 'Sum',
Min([KiloWatt]) as 'Min',
Max([KiloWatt]) as 'Max',
Sum([KiloWatt])/COUNT([KiloWatt]) as 'Average',
CONVERT(date, [DateTime])
from OneHourElectricitys
where [DateTime] < SYSDATETIME()
group by CONVERT(date, [DateTime])
EF 代码
var analytics = await _analyticsRepository.Query().Where(x => x.DateTime.Subtract(DateTime.Today).Days < 0)
.GroupBy(x => x.DateTime.Date)
.Select(x => new
{ DateTime = x.Key,
Max = x.Max(y => y.KiloWatt),
Min = x.Min(y => y.KiloWatt),
Avg = x.Sum(y => y.KiloWatt)/ x.Count(),
Sum = x.Sum(y => y.KiloWatt)
})
.ToListAsync();
堆栈跟踪:-
System.ArgumentException: must be reducible node at System.Linq.Expressions.Expression.ReduceAndCheck() at System.Linq.Expressions.Expression.ReduceExtensions()
我是不是做错了?
-- 更新--
以下 2 个 EF 查询 运行 没问题,但合并它们会出现错误。
var analytics = await _analyticsRepository.Query().Where(x => x.DateTime.Subtract(DateTime.Today).Days < 0)
var analytics = await _analyticsRepository.Query().GroupBy(x => x.DateTime.Date)
.Select(x => new
{ DateTime = x.Key,
Max = x.Max(y => y.KiloWatt),
Min = x.Min(y => y.KiloWatt),
Avg = x.Sum(y => y.KiloWatt)/ x.Count(),
Sum = x.Sum(y => y.KiloWatt)
})
.ToListAsync();
这是我的解决方案并且有效。
var analytics = await (_analyticsRepository.Query()
.Where(x => x.PanelId.Equals(panelId, StringComparison.CurrentCultureIgnoreCase)
&& DateTime.Compare(x.DateTime, new DateTime()) < 0))
.GroupBy(x => x.DateTime.Date)
.Select(x => new
{
DateTime = x.Key,
Max = x.Max(y => y.KiloWatt),
Min = x.Min(y => y.KiloWatt),
Avg = x.Sum(y => y.KiloWatt) / x.Count(),
Sum = x.Sum(y => y.KiloWatt)
}).ToListAsync();
谢谢,
如 Ivan Stoev 所述,此问题的正确解决方案是升级到 EF Core 2.1.3
使用
DateTimeField.Year +""+ DateTimeField.Month //Use this
而不是 使用自定义日期格式,例如
DateTimeField.ToString("yyyy - MM") //Don't use this
为我解决了问题
顺便说一句,我的 .net 核心版本是 2。2.x