Tengo un error al ejecutar esta consulta, en net core 3.1, pero en net core 2.1 corre sin problemas.
Al quitar "data" del select new, no muestra error, así que el error podría estar en "DateOfIssue", he revisado ese campo y ninguno es nulo.
Estoy utilizando net core 3.1.
Estoy consultando una lista de documentos, lo primero que hago es agruparlos en el tipo de documento y en cada tipo de documento los agrupo por día Esto es lo que espero recibir.
Tablas
CREATE TABLE [dbo].[documents](
[id] [int] IDENTITY(1,1) NOT NULL,
[date_of_issue] [date] NOT NULL DEFAULT (getdate()),
[document_type_id] [varchar](255) NOT NULL,
[total] [decimal](18, 2) NULL,
)
CREATE TABLE [dbo].[cat_document_types](
[id] [varchar](255) NOT NULL,
[description] [varchar](255) NULL,
[short] [varchar](255) NULL,
[active] [bit] NOT NULL DEFAULT ((1)),
[created_at] [date] NOT NULL DEFAULT (getdate()),
[updated_at] [date] NOT NULL DEFAULT (getdate()),
)
Resultado
[{
id = 1,
label = "Factura",
total = 20,
data :[{day : 1 , total : 10 },{ day : 2 , total : 10}]
}]
Consulta
var query = (from pa in _context.Documents
group pa by new { pa.DocumentTypeId, pa.DocumentType.Description } into grp
select
new
{
id = grp.Key.DocumentTypeId,
label = grp.Key.Description,
total = grp.Sum(x => x.Total),
data = (from pa in grp
group pa by new { pa.DateOfIssue.Day } into grp2
select new
{
day = grp2.Key.Day,
total = grp2.Sum(x => x.Total)
}).OrderBy(x => x.day)
}).ToList();
Error
System.ArgumentException: Argument type 'System.Linq.IOrderedQueryable`1[<>f__AnonymousType93`2[System.Int32,System.Nullable`1[System.Decimal]]]' does not match the corresponding member type 'System.Linq.IOrderedEnumerable`1[<>f__AnonymousType93`2[System.Int32,System.Nullable`1[System.Decimal]]]' (Parameter 'arguments[3]')
at System.Linq.Expressions.Expression.ValidateNewArgs(ConstructorInfo constructor, ReadOnlyCollection`1& arguments, ReadOnlyCollection`1& members)
at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments, IEnumerable`1 members)
at System.Linq.Expressions.NewExpression.Update(IEnumerable`1 arguments)
at System.Linq.Expressions.ExpressionVisitor.VisitNew(NewExpression node)
at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression`1 node)
at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at FacturadorSunatPro.API.Services.DashboardService.Report(DashboardDto dashboardDto) in C:\Users\MIGUEL\source\repos\FacturadorSunatProd.API\FacturadorSunatProd.API\Services\DashboardService.cs:line 27
at FacturadorSunatPro.API.Controllers.DashboardController.Data(DashboardDto dashboardDto) in C:\Users\MIGUEL\source\repos\FacturadorSunatProd.API\FacturadorSunatProd.API\Controllers\DashboardController.cs:line 48
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
group pa by new { pa.DateOfIssue.Day } into grp2
porgroup pa by pa.DateOfIssue.Day into grp2
? Probablemente el tipo anomino este causando ese error.pa.DocumentTypeId.value
, en mi caso es difícil poder hacer pruebas a tu código, quizás si haces un réplica y los subes a tu repositorio seria más fácil poder ayudartehttps://stackoverflow.com/questions/3007746/linq-argument-types-do-not-match-error-what-does-it-mean-how-do-i-address-i/3008006