实体核心2.0.3例外:必须是可还原节点

时间:2018-09-23 09:35:54

标签: c# entity-framework entity-framework-core ef-core-2.0

我正在尝试将以下SQL查询转换为EF

sql查询

Glide.with(getApplicationContext())
     .load("your url")
     .asBitmap()
     .into(new BitmapImageViewTarget(imgView) {
      @Override
      protected void setResource(Bitmap resource) {
       //Play with bitmap
        super.setResource(resource);
      }
    });

EF代码

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])

堆栈跟踪:-

  

System.ArgumentException:必须是可简化的节点      在System.Linq.Expressions.Expression.ReduceAndCheck()      在System.Linq.Expressions.Expression.ReduceExtensions()

我做错了吗?

-更新-

以下2个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();

3 个答案:

答案 0 :(得分:0)

这是我的解决方案,并且有效。

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();

谢谢

答案 1 :(得分:0)

此问题的正确解决方案是按照Ivan Stoev所述升级到EF Core 2.1.3

答案 2 :(得分:0)

使用

 DateTimeField.Year +""+ DateTimeField.Month  //Use this 

代替对日期等使用自定义格式

 DateTimeField.ToString("yyyy - MM") //Don't use this

为我解决了这个问题

我的.net核心版本是2.2.x