I've done a query with entity framework and I'm having performance issues.
public static Decimal ValorPrevisto(this ClinicaModel clinica)
{
return clinica.Contratos.Sum(c => c.Mensalidades.Where(m => m.Pagamento != null).Sum(t => t.Valor)) / 2;
}
This code is taking more than 3 minutes to run because it is scanning per record, I searched several times and could not find a way to improve performance.
ClinicalModel class
public class ClinicaModel : Base.BaseModel
{
public string Nome { get; set; }
public DateTime HoraExpedienteInicio { get; set; }
public DateTime HoraExpedienteTermino { get; set; }
public int DuracaoConsulta { get; set; }
public int DiasExpediente { get; set; }
public string Email { get; set; }
public virtual EnderecoModel Endereco { get; set; }
public virtual ContatoModel Telefone { get; set; }
public virtual PessoaFisicaModel Responsavel { get; set; }
public virtual ICollection<UsuarioModel> Usuarios { get; set; }
public virtual ICollection<DoutorModel> Doutores { get; set; }
public virtual ICollection<ConvenioModel> Convenios { get; set; }
public virtual ICollection<ContratoModel> Contratos { get; set; }
public virtual ICollection<PagamentoModel> Pagamentos { get; set; }
public ClinicaModel()
{
Usuarios = new HashSet<UsuarioModel>();
Doutores = new HashSet<DoutorModel>();
Convenios = new HashSet<ConvenioModel>();
Pagamentos = new HashSet<PagamentoModel>();
Contratos = new HashSet<ContratoModel>();
}
public override string ToString()
{
return Nome;
}
}