I am sending a model simplified.
See if you can help.
public IEnumerable<DTOCompanyDataWithSumValue> GetCompanyFinancesWithoutValues(DTOParameter parameter)
{
return Session.QueryOver<Company>(() => companyAlias)
.Where(x => x.Month == parameter.Month)
.And(x => x.Year == parameter.Year)
.WithSubquery.WhereExists(QueryOver.Of<Company>(() => companyAlias2)
.Where(e => e.KeyCompany == companyAlias.KeyCompany)
.And(x => x.Month == parameter.Month)
.And(x => x.Year == parameter.Year)
.JoinQueryOver(x => x.Financial, JoinType.LeftOuterJoin)
.JoinQueryOver(x => x.FinanceRegions, () => financesRegionsAlias, JoinType.LeftOuterJoin) //List -- This may contain records with zero or may not contain records
.Where(Restrictions.IsNull(Projections.Sum<FinanceRegion>(x => x.Value)) || Restrictions.Eq(Projections.Sum<FinanceRegion>(x => x.Value), 0))
.Select(Projections.Sum<FinanceRegion>(x => financeRegionsAlias.Value).WithAlias(() => dtoCompany.SumValue),
Projections.Group(() => companyAlias2.KeyCompany).WithAlias(() => dtoCompany.KeyCompany),
Projections.Group(() => companyAlias2.CompanyName).WithAlias(() => dtoCompany.CompanyName))
)
.TransformUsing(Transformers.AliasToBean<DTOCompanyDataWithSumValue>())
.List<DTOCompanyDataWithSumValue>().Distinct().ToList();
OR
return Session.QueryOver<Company>(() => companyAlias)
.Where(x => x.Month == parameter.Month)
.And(x => x.Year == parameter.Year)
.WithSubquery.WhereExists(QueryOver.Of<Company>(() => companyAlias2)
.Where(e => e.KeyCompany == companyAlias.KeyCompany)
.And(x => x.Month == parameter.Month)
.And(x => x.Year == parameter.Year)
.JoinQueryOver(x => x.Financial, JoinType.LeftOuterJoin)
.JoinQueryOver(x => x.FinanceRegions, () => financesRegionsAlias, JoinType.LeftOuterJoin) //List -- This may contain records with zero or may not contain records
.Where(Restrictions.Eq(Projections.Sum<FinanceRegion>(x => x.Value), 0)) || Restrictions.IsNull(Projections.Sum<FinanceRegion>((x) => x.Value)))
.Select(Projections.Sum<FinanceRegion>(x => financeRegionsAlias.Value).WithAlias(() => dtoCompany.SumValue),
Projections.Group(() => companyAlias2.KeyCompany).WithAlias(() => dtoCompany.KeyCompany),
Projections.Group(() => companyAlias2.CompanyName).WithAlias(() => dtoCompany.CompanyName))
)
.TransformUsing(Transformers.AliasToBean<DTOCompanyDataWithSumValue>())
.List<DTOCompanyDataWithSumValue>().Distinct().ToList();