public void UpdateFunctionPowers(int DepartmentID, int AppinfoID, int[] functionPowerIDList) { if (functionPowerIDList == null) { functionPowerIDList = new int[0]; } var oldFunctionPowers = GetPowers(DepartmentID, AppinfoID); int[] oldFunctionPowerIDList = oldFunctionPowers.Select(x => x.FunctionPowerID).ToArray(); var deleteFunctionPowers = oldFunctionPowerIDList.Except(functionPowerIDList); var insertFunctionPowers = functionPowerIDList.Except(oldFunctionPowerIDList); _dbHelper.BeginTran(); try { foreach (var deleteItem in deleteFunctionPowers) { var functionPower = oldFunctionPowers.Where(x => x.FunctionPowerID == deleteItem).First().ID; _dbHelper.DeleteAsTran <DepartmentFunctionPower>(new DepartmentFunctionPower { ID = functionPower }); } foreach (var insertItem in insertFunctionPowers) { _dbHelper.InsertAsTran <DepartmentFunctionPower>(new DepartmentFunctionPower { DepartmentID = DepartmentID, FunctionPowerID = insertItem }); } _dbHelper.CommitTran(); } catch (Exception ex) { _dbHelper.RollbackTran(); throw ex; } }