private void saveUserAccount(DataListItem dli) { HiddenField hdnAccountId = dli.FindControl("hdnAccountId") as HiddenField; int acctId = Convert.ToInt32(hdnAccountId.Value); AccountEntity acct = new AccountEntity(acctId); UserAccountEntity uae = new UserAccountEntity(); if (_userAccounts.Where(x => x.AccountId == acct.AccountId).Count() > 0) { uae = _userAccounts.Where(x => x.AccountId == acct.AccountId).First(); } CheckBox chkAccount = dli.FindControl("chkAccount") as CheckBox; if (chkAccount.Checked) { if (uae.IsNew) { uae.UserId = _user.UserId; uae.AccountId = acct.AccountId; uae.Save(); } } else { if (!uae.IsNew) { _userAccounts.Remove(uae); uae.Delete(); } } }