I would like to make a COUNT
using LINQ / LAMBDA, but I'm having difficulties. Can you help me?
The idea is to reproduce the following query:
SELECT Conta, Tipo, Documento, Nome, COUNT(0) AS Qtde
FROM TaxaPrecificacao
GROUP BY Conta, Tipo, Documento, Nome
Here are some ways I tried to do it, but of course all are wrong.
//Funciona mas a coluna do Count não está sendo calculada
var view = TaxaPrecificacao
.Select(
x => new PrecTxContas
{
Conta = x.Conta,
Tipo = x.Tipo,
Documento = x.Documento,
Nome = x.Nome,
Qtde = 0
});
//Erro de sintaxe
var view = TaxaPrecificacao
.Select(
x => new PrecTxContas
{
Conta = x.Conta,
Tipo = x.Tipo,
Documento = x.Documento,
Nome = x.Nome,
Qtde = x.Count()
});
/*Sintaxe OK mas não compila The type appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order*/
var view = TaxaPrecificacao
.GroupBy(
x => new PrecTxContas
{
Conta = x.Conta,
Tipo = x.Tipo,
Documento = x.Documento,
Nome = x.Nome
})
.Select
(
x => new PrecTxContas
{
Conta = x.Conta,
Tipo = x.Tipo,
Documento = x.Documento,
Nome = x.Nome,
Qtde = x.Count()
});