I have the following query in LINQ
var dados = _db.Contratos
.Where(a => a.Adesoes.Any(b => b.Pago))
.Select(a => new
{
Contrato = a.Numero,
ValorTASenior = a.Adesoes.Where(b => b.Pago).Sum(b => b.ValorTASenior),
ValorTAMaster = a.Adesoes.Where(b => b.Pago).Sum(b => b.ValorTAMaster),
ValorTAConsultor = a.Adesoes.Where(b => b.Pago).Sum(b => b.ValorTAConsultor),
ValorCliente = a.Adesoes.Where(b => b.Pago).Sum(b => b.ValorCliente)
});
I would like to know how I can simplify the sum of the values and avoid the a.Adesoes.Where(b => b.Pago)
for each sum that I need to do.
Entities & Context
public class MeuContext : DbContext
{
public DbSet<Contrato> Contratos { get; set; }
}
public class Contrato
{
[Key]
public int ContratoId { get; set; }
public int Numero { get; set; }
public virtual ICollection<Adesao> Adesoes { get; set; }
}
public class Adesao
{
[Key]
public int AdesaoId { get; set; }
public int ContratoId { get; set; }
public bool Pago { get; set; }
public int ValorTASenior { get; set; }
public int ValorTAMaster { get; set; }
public int ValorTAConsultor { get; set; }
public int ValorCliente { get; set; }
public virtual Contrato Contrato { get; set; }
}