Must be reducible node

Can't get MonthleStats for RadzenCRM demo. Getting "Must be reducible node" at the breakpoint.
var stats = context.Opportunities
.Include(opportunity => opportunity.OpportunityStatus)
.Where(opportunity => opportunity.OpportunityStatus.Name == "Won")
.GroupBy(opportunity => new DateTime(opportunity.CloseDate.Year, opportunity.CloseDate.Month, 1))
.Select(group => new
{
Month = group.Key,
Revenue = group.Sum(opportunity => opportunity.Amount),
Opportunities = group.Count(),
AverageDealSize = group.Average(opportunity => opportunity.Amount),
Ratio = ratio
})
.OrderBy(deals => deals.Month)
.Last();

This is a strange error indeed. There are similar reports but they don't sound related. It could be that your database doesn't have enough data (or no opportunity status which is 'Won'?).

Anyway you can try calling ToList() right before GroupBy() to eliminate entity framework issues:

var stats = context.Opportunities
.Include(opportunity => opportunity.OpportunityStatus)
.Where(opportunity => opportunity.OpportunityStatus.Name == "Won")
.ToList() // <--- new code
.GroupBy(opportunity => new DateTime(opportunity.CloseDate.Year, opportunity.CloseDate.Month, 1))
.Select(group => new
{
Month = group.Key,
Revenue = group.Sum(opportunity => opportunity.Amount),
Opportunities = group.Count(),
AverageDealSize = group.Average(opportunity => opportunity.Amount),
Ratio = ratio
})
.OrderBy(deals => deals.Month)
.Last();

I have OpportunityStatus because I run the SQL script for me to get more data for RadzenCRM dashboard. I did the equivalent code using SQL and it can get the data just fine.

SELECT SUM(Opportunities.Amount) AS Amount,
Opportunities.CloseDate
FROM Opportunities
LEFT JOIN OpportunityStatuses
ON Opportunities.StatusId = OpportunityStatuses.Id
WHERE OpportunityStatuses.Name = 'Won'
GROUP BY Opportunities.CloseDate

SELECT TOP(1) SUM(Opportunities.Amount) AS Amount,
CloseDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, Opportunities.CloseDate), 0)
FROM Opportunities
LEFT JOIN OpportunityStatuses
ON Opportunities.StatusId = OpportunityStatuses.Id
WHERE OpportunityStatuses.Name = 'Won'
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Opportunities.CloseDate), 0)
ORDER BY CloseDate

I been reading on the net too that the error is pertaining to EF core and it complex select statement, but I see that https://crm.radzen.com works just fine.

Add the StackTrace
StackTrace " at System.Linq.Expressions.Expression.ReduceAndCheck()\r\n at System.Linq.Expressions.Expression.ReduceExtensions()\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberExpression(Expression expr, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression1 lambda)\r\n at System.Linq.Expressions.Expression1.Accept(StackSpiller spiller)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteNewExpression(Expression expr, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression1 lambda)\r\n at System.Linq.Expressions.Expression1.Accept(StackSpiller spiller)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)\r\n at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression1 lambda)\r\n at System.Linq.Expressions.Expression1.Accept(StackSpiller spiller)\r\n at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)\r\n at System.Linq.Expressions.Expression1.Compile(Boolean preferInterpretation)\r\n at System.Linq.Expressions.Expression1.Compile()\r\n at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambdaTResults\r\n at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CreateExecutorLambdaTResults\r\n at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)\r\n at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger1 logger, Type contextType)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_01.b__0()\r\n at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n at Remotion.Linq.QueryableBase1.GetEnumerator()\r\n at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)\r\n at RadzenCrm.Controllers.ServerMethodsController.MonthlyStats() in C:\Learning\Radzen\RadzenCRM\server\Controllers\ServerMethodsController.cs:line 40\r\n at lambda_method(Closure , Object , Object[] )\r\n at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)\r\n at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()" string

@HotRice I've managed to reproduce the error and it seems to be a regression in Entity Framework (due for a fix in 3.0.0). The suggested workaround is to call ToList() after GroupBy:

var stats = context.Opportunities
                    .Include(opportunity => opportunity.OpportunityStatus)
                    .Where(opportunity => opportunity.OpportunityStatus.Name == "Won")
                    .GroupBy(opportunity => new DateTime(opportunity.CloseDate.Year, opportunity.CloseDate.Month, 1))
                    .ToList() // <--- Suggested workaround
                    .Select(group => new
                    {
                        Month = group.Key,
                        Revenue = group.Sum(opportunity => opportunity.Amount),
                        Opportunities = group.Count(),
                        AverageDealSize = group.Average(opportunity => opportunity.Amount),
                        Ratio = ratio
                    })
                    .OrderBy(deals => deals.Month)
                    .Last();

I tested it and it seems to work fine. According to the Microsoft guys the previous (working) versions called ToList internally and this is why it used to work. The hosted version of the CRM demo was created with .NET Core 2.0 which is why the problem doesn't manifest. We will update the code in the documentation to include the ToList() call.

Thank you for your patience.