public List <TenantPayment> GetPayments(int billingCycleId) { var data = new PaymentData().GetPayments(billingCycleId); var tenants = data.GroupBy(p => p.TenantId).ToList(); var tenantPayments = new List <TenantPayment>(); tenants.ForEach(t => { var payments = t.ToList(); var tenantPayment = new TenantPayment() { Payment = new Payment() { Amount = 0, Balance = 0 }, Tenant = new Tenant() }; payments.ForEach(p => { if (p.Payment == null) { p.Payment = new Payment() { Amount = 0, BillingCyclePaidId = 0 }; } tenantPayment.Tenant = p.Tenant; tenantPayment.Payment.BillingCyclePaidId = p.Payment.BillingCyclePaidId; if (tenantPayment.Payment.BillingCyclePaidId == billingCycleId) { tenantPayment.Payment.Amount += p.Payment.Amount; } }); tenantPayments.Add(tenantPayment); }); tenantPayments.ForEach(tp => { tp.Payment.Balance = Convert.ToDecimal(tp.Tenant.Apartment.RentCost) - tp.Payment.Amount; }); return(tenantPayments); }