public static List <BranchAdminView> GetBranchAdminViewList(ApplicationDbContext db, IPrincipal user) { List <BranchAdminView> branchAdminViewList = new List <BranchAdminView>(); //Get the AppUser of this User AppUser appUser = AppUserHelpers.GetAppUser(db, user); //Get the list of branches this User is linked to List <Branch> branchesForAppUser = BranchHelpers.GetBranchesForUserForAdminView(db, appUser.AppUserId); //Build a list of all users that are linked to this company List <AppUser> allUsersForCompany = AppUserHelpers.GetAppUsersForCompany(branchesForAppUser[0].CompanyId); foreach (Branch branch in branchesForAppUser) { //Build list of company users to add to this branchView List <BranchAdminViewCompanyUser> branchAdminCompanyUsers = new List <BranchAdminViewCompanyUser>(); //Build a list of all users for this branch List <AppUser> allUsersForThisBranch = (from bu in db.BranchUsers join au in db.AppUsers on bu.UserId equals au.AppUserId where (bu.BranchId == branch.BranchId && bu.EntityStatus == EntityStatusEnum.Active) select au).Distinct().ToList(); //Add all company users to the 'RelatedCompanyUsers' and set the flag to true if the user appears in the branch list List <BranchAdminViewCompanyUser> relatedCompanyUsers = new List <BranchAdminViewCompanyUser>(); foreach (AppUser userForCompany in allUsersForCompany) { UserRoleEnum?role = null; //If the user appears in branchlist then set the 'linked' flag bool found = false; AppUser foundUser = allUsersForThisBranch.FirstOrDefault(x => x.AppUserId == userForCompany.AppUserId); if (foundUser != null) { //get role from branchuser for this userForCompany/branch BranchUser branchUserForCompany = BranchUserHelpers.GetBranchUser(db, userForCompany.AppUserId, branch.BranchId, branch.CompanyId); role = branchUserForCompany.UserRole; found = true; } BranchAdminViewCompanyUser branchAdminCompanyUser = new BranchAdminViewCompanyUser() { AppUserId = userForCompany.AppUserId, CurrentBranchId = userForCompany.CurrentBranchId, FirstName = userForCompany.FirstName, LastName = userForCompany.LastName, UserRole = role, EntityStatus = userForCompany.EntityStatus, LinkedToThisBranch = found }; branchAdminCompanyUsers.Add(branchAdminCompanyUser); } //Add this list of users (as UserAdminRelatedBranchView(s)) to the model BranchAdminView branchAdminView = new BranchAdminView() { BranchId = branch.BranchId, CompanyId = branch.CompanyId, BranchName = branch.BranchName, BusinessType = branch.BusinessType, AddressLine1 = branch.AddressLine1, AddressLine2 = branch.AddressLine2, AddressLine3 = branch.AddressLine3, AddressTownCity = branch.AddressTownCity, AddressCounty = branch.AddressCounty, AddressPostcode = branch.AddressPostcode, TelephoneNumber = branch.TelephoneNumber, Email = branch.Email, ContactName = branch.ContactName, PrivacyLevel = branch.PrivacyLevel, EntityStatus = branch.EntityStatus, CompanyUserListId = Guid.NewGuid(), //used to identify the following list in the view (can't use BranchId as that is used for a different block in the table) RelatedCompanyUsers = branchAdminCompanyUsers }; branchAdminViewList.Add(branchAdminView); } return(branchAdminViewList); }