public MarginLendingAccountPortfolioDetails GenerateAccountPortfolioDetailsModel(string accountId, string accountCatergory) { MarginLendingAccountPortfolioDetails model = new MarginLendingAccountPortfolioDetails { data = new List <MarginLendingAccountPortfolioItem>(), marginLenders = new List <MarginLendersDetails>() }; AccountBase account = null; if (accountCatergory == AccountCatergories.ClientAccount.ToString()) { account = edisRepo.GetClientAccountById(accountId); } else if (accountCatergory == AccountCatergories.GroupAccount.ToString()) { account = edisRepo.GetGroupAccountById(accountId); } List <AssetBase> allEquities = account.GetEquities().ToList(); allEquities.ForEach(e => { Equity equity = (Equity)e; MarginLending lending = edisRepo.GetMarginLendingForAccountAsset(e.Id, account.AccountNumber); double?maxRatio = edisRepo.GetMaxRatio(equity.Ticker, account.MarginLenderId); double marketValue = e.GetTotalMarketValue(); if (lending != null) { model.data.Add(new MarginLendingAccountPortfolioItem { ticker = equity.Ticker, companyName = equity.Name, marketValue = marketValue, loanAmount = lending.LoanAmount, loanValueRatio = lending.LoanValueRatio * 100, maxLoanValueRatio = maxRatio == null ? 0 : (double)maxRatio * 100, netCostValue = lending.NetCostValue }); } else { model.data.Add(new MarginLendingAccountPortfolioItem { ticker = equity.Ticker, companyName = equity.Name, marketValue = marketValue, loanAmount = 0, loanValueRatio = 0, maxLoanValueRatio = maxRatio == null ? 0 : (double)maxRatio * 100, netCostValue = marketValue }); } var marginLenders = edisRepo.GetMarginLendersByTicker(equity.Ticker); marginLenders.ForEach(l => { model.marginLenders.Add(new MarginLendersDetails { companyName = l.LenderName, maxLoanValueRatio = l.Ratios.Count == 0 ? 0 : l.Ratios.OrderByDescending(r => r.CreatedOn).SingleOrDefault().MaxRatio * 100, ticker = equity.Ticker }); }); }); return(model); }