Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Core 2.0.3 Exception: must be reducible node

I am trying to convert the following sql query to EF

sql Query

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 Code

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

Stack Trace :-

System.ArgumentException: must be reducible node at System.Linq.Expressions.Expression.ReduceAndCheck() at System.Linq.Expressions.Expression.ReduceExtensions()

Am I doing coming wrong?

-- Update --

The following 2 EF queries run fine but combining them gives the error.

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();
like image 716
23nigam Avatar asked Dec 05 '25 13:12

23nigam


2 Answers

The correct solution to this problem is to upgrade to EF Core 2.1.3 as mentioned by Ivan Stoev

like image 147
23nigam Avatar answered Dec 07 '25 09:12

23nigam


This is my solution and it works.

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

Thanks,

like image 43
Do Tat Hoan Avatar answered Dec 07 '25 11:12

Do Tat Hoan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!