4
respostas

Erro ao utilizar let no meio da consulta

Estou tentando executar o seguinte código no console:

 using (var context = new AluraTunesContext())
            {
                var query = from nf in context.ItemNotaFiscals
                            where nf.Faixa.Album.Artista.Nome == "Led Zeppelin"

                            group nf by nf.Faixa.Album.Titulo
                                into agrupado

                            let total = agrupado.Sum(g => g.Quantidade * g.PrecoUnitario)

                            orderby total
                                descending

                            select new
                            {
                                Album = agrupado.Key,
                                Quantidade = agrupado.Sum(g => g.Quantidade),
                                Preco = total
                            };



                foreach (var nf in query)
                {
                    Console.WriteLine("{0}\t{1}\t{2}", nf.Album.PadRight(40), nf.Quantidade, nf.Preco);
                }


            }

Estou utilizando .Net5 EF core, quando executo da o seguinte erro:

System.InvalidOperationException: 'Processing of the LINQ expression 'GroupByShaperExpression:
KeySelector: a.Titulo, 
ElementSelector:EntityShaperExpression: 
    EntityType: ItemNotaFiscal
    ValueBufferExpression: 
        ProjectionBindingExpression: EmptyProjectionMember
    IsNullable: False
' by 'RelationalProjectionBindingExpressionVisitor' failed. This may indicate either a bug or a limitation in Entity Framework. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.'

Se executo a mesma consulta sem o LET funciona, como segue:

using (var context = new AluraTunesContext())
            {
                var query = from nf in context.ItemNotaFiscals
                            where nf.Faixa.Album.Artista.Nome == "Led Zeppelin"

                            group nf by nf.Faixa.Album.Titulo
                                into agrupado

                            orderby agrupado.Sum(g => g.Quantidade * g.PrecoUnitario)
                                descending

                            select new
                            {
                                Album = agrupado.Key,
                                Quantidade = agrupado.Sum(g => g.Quantidade),
                                Preco = agrupado.Sum(g => g.Quantidade * g.PrecoUnitario)
                            };



                foreach (var nf in query)
                {
                    Console.WriteLine("{0}\t{1}\t{2}", nf.Album.PadRight(40), nf.Quantidade, nf.Preco);
                }


            }
4 respostas

Opa, tenta fazer assim

 let total = agrupado.Sum(g => (g.Quantidade * g.PrecoUnitario))

faz nos outros também

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

Oi obrigado pela dica, mas o problema persiste.

coloca um print do conteúdo da variavel query

Vou fazer melhor, segue o projeto para tentar me ajudar.

Obrigado pela disposição.

https://drive.google.com/drive/folders/1qphyGpFOvlNNWJW3ZhFSG_ZEbcOrzl8c?usp=sharing

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software