/// <summary> /// Overrides the creation of <see cref="ICustomerAddress"/>. /// </summary> /// <param name="model"> /// The <see cref="ICustomerAddress"/>. /// </param> /// <param name="adr"> /// The <see cref="FastTrackBillingAddressModel"/>. /// </param> /// <param name="customer"> /// The <see cref="ICustomer"/>. /// </param> /// <param name="label"> /// The customer address label (e.g. Billing Address). /// </param> /// <param name="addressType"> /// The <see cref="AddressType"/>. /// </param> /// <returns> /// The modified <see cref="ICustomerAddress"/>. /// </returns> protected override ICustomerAddress OnCreate(ICustomerAddress model, FastTrackBillingAddressModel adr, ICustomer customer, string label, AddressType addressType) { // Set the address to the default address model.IsDefault = true; return(base.OnCreate(model, adr, customer, label, addressType)); }
public static ICustomerAddress MockSavedWithKey(this ICustomerAddress entity, Guid key) { entity.Key = key; ((Entity)entity).AddingEntity(); entity.ResetDirtyProperties(); return(entity); }
/// <summary> /// Saves a single <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="raiseEvents"> /// Optional boolean indicating whether or not to raise events /// </param> public void Save(ICustomerAddress address, bool raiseEvents = true) { if (raiseEvents) { if (Saving.IsRaisedEventCancelled(new SaveEventArgs <ICustomerAddress>(address), this)) { ((CustomerAddress)address).WasCancelled = true; return; } } var count = GetCustomerAddressCount(address.CustomerKey, address.AddressType); if (count == 0 || address.IsDefault) { ClearDefaultCustomerAddress(address.CustomerKey, address.AddressType); address.IsDefault = true; } using (new WriteLock(Locker)) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateCustomerAddressRepository(uow)) { repository.AddOrUpdate(address); uow.Commit(); } } if (raiseEvents) { Saved.RaiseEvent(new SaveEventArgs <ICustomerAddress>(address), this); } }
// method to get customers public static CustomerAddress[] List(string apiUrl, string sessionId, object[] args) { ICustomerAddress proxyCustomer = (ICustomerAddress)XmlRpcProxyGen.Create(typeof(ICustomerAddress)); proxyCustomer.Url = apiUrl; return(proxyCustomer.List(sessionId, _customer_list, args)); }
// method to delete a customer address public static bool Delete(string apiUrl, string sessionId, int customerAddressid) { ICustomerAddress proxyCustomer = (ICustomerAddress)XmlRpcProxyGen.Create(typeof(ICustomerAddress)); proxyCustomer.Url = apiUrl; return(proxyCustomer.Update(sessionId, _customer_delete, new object[] { customerAddressid })); }
// method to create a customer address public static string Create(string apiUrl, string sessionId, CustomerAddress customer) { ICustomerAddress proxyCustomer = (ICustomerAddress)XmlRpcProxyGen.Create(typeof(ICustomerAddress)); proxyCustomer.Url = apiUrl; return(proxyCustomer.Create(sessionId, _customer_create, new object[] { customer })); }
/// <summary> /// The to customer address display. /// </summary> /// <param name="customerAddress"> /// The customer address. /// </param> /// <returns> /// The <see cref="CustomerAddressDisplay"/>. /// </returns> public static CustomerAddressDisplay ToCustomerAddressDisplay(this ICustomerAddress customerAddress) { var customer = AutoMapper.Mapper.Map <CustomerAddressDisplay>(customerAddress); var country = MerchelloConfiguration.Current.MerchelloCountries().Countries.FirstOrDefault(x => x.CountryCode.Equals(customer.CountryCode, StringComparison.InvariantCultureIgnoreCase)); if (country != null) { customer.CountryName = country.Name; } return(customer); }
public ActionResult Delete(Guid key) { var addresses = Customer.Addresses.ToList(); addresses.RemoveAll(x => x == null); ICustomerAddress address = addresses.FirstOrDefault(x => x.Key == key); if (address != null) { Customer.DeleteCustomerAddress(address); } return(RedirectToAction("Index")); }
/// <summary> /// Maps a <see cref="ICustomerAddress"/> to a <see cref="IAddress"/>. /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="name"> /// The name. /// </param> /// <returns> /// The <see cref="IAddress"/>. /// </returns> public static IAddress AsAddress(this ICustomerAddress address, string name) { return(new Address() { Name = name, Organization = address.Company, Address1 = address.Address1, Address2 = address.Address2, Locality = address.Locality, Region = address.Region, PostalCode = address.PostalCode, CountryCode = address.CountryCode, Phone = address.Phone, AddressType = address.AddressType }); }
/// <summary> /// Maps an <see cref="ICustomerAddress"/> to <see cref="AddressModel"/> /// </summary> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="AddressModel"/>. /// </returns> public static AddressModel ToAddress(this ICustomerAddress address) { return(new AddressModel(address.CustomerKey, address.Key) { Address1 = address.Address1, Address2 = address.Address2, CountryCode = address.CountryCode, // Email = address.Email, //AddressUsageRole = address.Label.IsCommercial ? AddressModel.AddressRole.Business : AddressModel.AddressRole.Residential, Alias = address.Label, City = address.Locality, Fullname = address.FullName, Phone = address.Phone, PostalCode = address.PostalCode, Region = address.Region }); }
/// <summary> /// Creates a <see cref="ICheckoutAddressModel"/> from a <see cref="ICustomerAddress"/>. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="adr"> /// The <see cref="ICustomerAddress"/>. /// </param> /// <returns> /// The <see cref="ICheckoutAddressModel"/>. /// </returns> public TAddress Create(ICustomer customer, ICustomerAddress adr) { var model = new TAddress { Name = adr.FullName, Organization = adr.Company, Email = customer.Email, Address1 = adr.Address1, Address2 = adr.Address2, Locality = adr.Locality, Region = adr.Region, CountryCode = adr.CountryCode, PostalCode = adr.PostalCode, Phone = adr.Phone, AddressType = adr.AddressType, }; return(OnCreate(model, customer, adr)); }
/// <summary> /// Deletes a single <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="raiseEvents"> /// The raise events. /// </param> public void Delete(ICustomerAddress address, bool raiseEvents = true) { if (raiseEvents) { Deleting.RaiseEvent(new DeleteEventArgs <ICustomerAddress>(address), this); } using (new WriteLock(Locker)) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateCustomerAddressRepository(uow)) { repository.Delete(address); uow.Commit(); } } if (raiseEvents) { Deleted.RaiseEvent(new DeleteEventArgs <ICustomerAddress>(address), this); } // If we deleted the default address and there are other addresses of this type - pick one if (!address.IsDefault) { return; } var newDefault = GetByCustomerKey(address.CustomerKey, address.AddressType).FirstOrDefault(); if (newDefault == null) { return; } newDefault.IsDefault = true; Save(newDefault); }
/// <summary> /// Saves customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> internal static ICustomerAddress SaveCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); var addressList = new List <ICustomerAddress>(); var addresses = customer.Addresses.ToList(); var isUpdate = false; foreach (var adr in addresses) { if (address.IsDefault && adr.Key != address.Key && adr.AddressType == address.AddressType) { adr.IsDefault = false; } if (addresses.Any(x => x.Key == address.Key)) { isUpdate = true; } addressList.Add(adr); } if (!isUpdate) { addresses.Add(address); } ((Customer)customer).Addresses = addresses; merchelloContext.Services.CustomerService.Save(customer); return(address); }
/// <summary> /// Saves a single <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address to be saved /// </param> public void Save(ICustomerAddress address) { _customerAddressService.Save(address); }
/// <summary> /// The <see cref="ICustomerAddress"/> to be saved /// </summary> /// <param name="customer"> /// The customer associated with the address /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> public static ICustomerAddress SaveCustomerAddress(this ICustomer customer, ICustomerAddress address) { return(customer.SaveCustomerAddress(MerchelloContext.Current, address)); }
/// <summary> /// Deletes a customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="address"> /// The address to be deleted /// </param> public static void DeleteCustomerAddress(this ICustomer customer, ICustomerAddress address) { customer.DeleteCustomerAddress(MerchelloContext.Current, address); }
/// <summary> /// The as customer address. /// </summary> /// <param name="adr"> /// The adr. /// </param> /// <param name="existing"> /// The existing. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> public static ICustomerAddress AsCustomerAddress(this CustomerAddressModel adr, ICustomerAddress existing) { existing.Label = adr.Label; existing.FullName = adr.FullName; existing.Company = adr.Company; existing.AddressType = adr.AddressType == "shipping" ? AddressType.Shipping : AddressType.Billing; existing.Address1 = adr.Address1; existing.Address2 = adr.Address2; existing.Locality = adr.Locality; existing.Region = adr.Region; existing.PostalCode = adr.PostalCode; existing.CountryCode = adr.CountryCode; existing.Phone = adr.Phone; return existing; }
/// <summary> /// Maps a <see cref="CustomerAddressDisplay"/> to a <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="destination"> /// The existing address to be updated /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> public static ICustomerAddress ToCustomerAddress(this CustomerAddressDisplay address, ICustomerAddress destination) { destination.FullName = address.FullName; destination.Label = address.Label; destination.Company = address.Company; destination.Address1 = address.Address1; destination.Address2 = address.Address2; destination.AddressTypeFieldKey = address.AddressTypeFieldKey; destination.AddressType = address.AddressType; destination.Region = address.Region; destination.Locality = address.Locality; destination.PostalCode = address.PostalCode; destination.CountryCode = address.CountryCode; destination.Company = address.Company; destination.Phone = address.Phone; destination.IsDefault = address.IsDefault; if (!address.Key.Equals(Guid.Empty)) destination.Key = address.Key; return destination; }
/// <summary> /// The <see cref="ICustomerAddress"/> to be saved /// </summary> /// <param name="customer"> /// The customer associated with the address /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> public static ICustomerAddress SaveCustomerAddress(this ICustomer customer, ICustomerAddress address) { return customer.SaveCustomerAddress(MerchelloContext.Current, address); }
/// <summary> /// Saves customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> internal static ICustomerAddress SaveCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); var addressList = new List<ICustomerAddress>(); var addresses = customer.Addresses.ToList(); var isUpdate = false; foreach (var adr in addresses) { if (address.IsDefault && adr.Key != address.Key && adr.AddressType == address.AddressType) adr.IsDefault = false; if (addresses.Any(x => x.Key == address.Key)) { isUpdate = true; } addressList.Add(adr); } if (!isUpdate) addresses.Add(address); ((Customer)customer).Addresses = addresses; merchelloContext.Services.CustomerService.Save(customer); return address; }
/// <summary> /// The to customer address display. /// </summary> /// <param name="customerAddress"> /// The customer address. /// </param> /// <returns> /// The <see cref="CustomerAddressDisplay"/>. /// </returns> public static CustomerAddressDisplay ToCustomerAddressDisplay(this ICustomerAddress customerAddress) { return(AutoMapper.Mapper.Map <CustomerAddressDisplay>(customerAddress)); }
/// <summary> /// The delete customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> internal static void DeleteCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); var addresses = customer.Addresses.Where(x => x.Key != address.Key).ToList(); if (addresses.Any(x => x.AddressType == address.AddressType) && address.IsDefault) { addresses.First(x => x.AddressType == address.AddressType).IsDefault = true; } ((Customer)customer).Addresses = addresses; merchelloContext.Services.CustomerService.Save(customer); }
/// <summary> /// The delete customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> internal static void DeleteCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); ((ServiceContext)merchelloContext.Services).CustomerAddressService.Delete(address); var addresses = customer.Addresses.ToList(); if (addresses.Any(x => x.Key == address.Key)) { addresses.RemoveAt(addresses.IndexOf(addresses.FirstOrDefault(x => x.Key == address.Key))); } ((Customer)customer).Addresses = addresses; }
/// <summary> /// Saves customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> internal static ICustomerAddress SaveCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); var addressList = new List <ICustomerAddress>(); // REFACTOR-v3 // Caching issue - customerAddresses can return with a null address in the list, usually caused from the // redundant cache item created by customer's being retrieved from the customer context. (Added note there). // HACK remove the null addresses here as are caused from items being removed without cache cleared correctly and // which will allow things to proceed normally (and the cache correct itself in a hacky way). var addresses = customer.Addresses.Where(x => x != null).ToList(); var isUpdate = false; foreach (var adr in addresses) { if (address.IsDefault && adr.Key != address.Key && adr.AddressType == address.AddressType) { adr.IsDefault = false; } if (addresses.Any(x => x.Key == address.Key)) { isUpdate = true; } addressList.Add(adr); } if (!isUpdate) { addresses.Add(address); } ((Customer)customer).Addresses = addresses; merchelloContext.Services.CustomerService.Save(customer); return(address); }
/// <summary> /// Allows for overriding the creation of <see cref="ICheckoutAddressModel"/> from <see cref="ICustomerAddress"/>. /// </summary> /// <param name="model"> /// The <see cref="ICheckoutAddressModel"/>. /// </param> /// <param name="customer"> /// The <see cref="ICustomer"/>. /// </param> /// <param name="adr"> /// The <see cref="ICustomerAddress"/>. /// </param> /// <returns> /// The modified <see cref="ICheckoutAddressModel"/>. /// </returns> protected virtual TAddress OnCreate(TAddress model, ICustomer customer, ICustomerAddress adr) { return(model); }
protected override TAddress OnCreate(TAddress model, ICustomer customer, ICustomerAddress adr) { model.Email = customer.Email; model.Countries = GetCountrySelectListItems(); return(base.OnCreate(model, customer, adr)); }
/// <summary> /// Saves customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> internal static ICustomerAddress SaveCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); ((ServiceContext)merchelloContext.Services).CustomerAddressService.Save(address); var addresses = customer.Addresses.ToList(); if (addresses.Any(x => x.Key == address.Key)) { addresses.RemoveAt(addresses.IndexOf(addresses.FirstOrDefault(x => x.Key == address.Key))); } addresses.Add(address); ((Customer)customer).Addresses = addresses; return address; }
/// <summary> /// Deletes a single instance of the <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address to be deleted /// </param> public void Delete(ICustomerAddress address) { _customerAddressService.Delete(address); }
/// <summary> /// The delete customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> internal static void DeleteCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); var addresses = customer.Addresses.Where(x => x.Key != address.Key).ToList(); if (addresses.Any(x => x.AddressType == address.AddressType) && address.IsDefault) addresses.First(x => x.AddressType == address.AddressType).IsDefault = true; ((Customer)customer).Addresses = addresses; merchelloContext.Services.CustomerService.Save(customer); }
/// <summary> /// Saves a single <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="raiseEvents"> /// Optional boolean indicating whether or not to raise events /// </param> public void Save(ICustomerAddress address, bool raiseEvents = true) { if (raiseEvents) if (Saving.IsRaisedEventCancelled(new SaveEventArgs<ICustomerAddress>(address), this)) { ((CustomerAddress)address).WasCancelled = true; return; } var count = GetCustomerAddressCount(address.CustomerKey, address.AddressType); if (count == 0 || address.IsDefault) { ClearDefaultCustomerAddress(address.CustomerKey, address.AddressType); address.IsDefault = true; } using (new WriteLock(Locker)) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateCustomerAddressRepository(uow)) { repository.AddOrUpdate(address); uow.Commit(); } } if (raiseEvents) Saved.RaiseEvent(new SaveEventArgs<ICustomerAddress>(address), this); }
/// <summary> /// The as customer address. /// </summary> /// <param name="adr"> /// The adr. /// </param> /// <param name="existing"> /// The existing. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> public static ICustomerAddress AsCustomerAddress(this CustomerAddressModel adr, ICustomerAddress existing) { existing.Label = adr.Label; existing.FullName = adr.FullName; existing.Company = adr.Company; existing.AddressType = adr.AddressType == "shipping" ? AddressType.Shipping : AddressType.Billing; existing.Address1 = adr.Address1; existing.Address2 = adr.Address2; existing.Locality = adr.Locality; existing.Region = adr.Region; existing.PostalCode = adr.PostalCode; existing.CountryCode = adr.CountryCode; existing.Phone = adr.Phone; return(existing); }
/// <summary> /// Allows for overriding the creation of <see cref="ICustomerAddress"/> from <see cref="ICheckoutAddressModel"/>. /// </summary> /// <param name="model"> /// The <see cref="ICustomerAddress"/>. /// </param> /// <param name="adr"> /// The <see cref="ICheckoutAddressModel"/>. /// </param> /// <param name="customer"> /// The <see cref="ICustomer"/>. /// </param> /// <param name="label"> /// The customer address label (e.g. My House). /// </param> /// <param name="addressType"> /// The <see cref="AddressType"/>. /// </param> /// <returns> /// The modified <see cref="ICustomerAddress"/>. /// </returns> protected virtual ICustomerAddress OnCreate(ICustomerAddress model, TAddress adr, ICustomer customer, string label, AddressType addressType) { return(model); }
/// <summary> /// Maps a <see cref="CustomerAddressDisplay"/> to a <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="destination"> /// The existing address to be updated /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> public static ICustomerAddress ToCustomerAddress(this CustomerAddressDisplay address, ICustomerAddress destination) { destination.FullName = address.FullName; destination.Label = address.Label; destination.Address1 = address.Address1; destination.Address2 = address.Address2; destination.AddressTypeFieldKey = address.AddressTypeFieldKey; destination.Region = address.Region; destination.Locality = address.Locality; destination.PostalCode = address.PostalCode; destination.CountryCode = address.CountryCode; destination.Company = address.Company; destination.Phone = address.Phone; destination.IsDefault = address.IsDefault; if (!address.Key.Equals(Guid.Empty)) { destination.Key = address.Key; } return(destination); }
/// <summary> /// Saves a customer address and updated the customer address collection on the customer. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="address"> /// The address. /// </param> private void SaveAddress(ICustomer customer, ICustomerAddress address) { _customerAddressService.Save(address); var addresses = customer.Addresses.ToList(); if (addresses.Any(x => x.Key == address.Key)) { addresses.RemoveAt(addresses.IndexOf(addresses.FirstOrDefault(x => x.Key == address.Key))); } addresses.Add(address); ((Customer)customer).Addresses = addresses; }
/// <summary> /// Deletes a single <see cref="ICustomerAddress"/> /// </summary> /// <param name="address"> /// The address. /// </param> /// <param name="raiseEvents"> /// The raise events. /// </param> public void Delete(ICustomerAddress address, bool raiseEvents = true) { if (raiseEvents) Deleting.RaiseEvent(new DeleteEventArgs<ICustomerAddress>(address), this); using (new WriteLock(Locker)) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateCustomerAddressRepository(uow)) { repository.Delete(address); uow.Commit(); } } if (raiseEvents) Deleted.RaiseEvent(new DeleteEventArgs<ICustomerAddress>(address), this); // If we deleted the default address and there are other addresses of this type - pick one if (!address.IsDefault) return; var newDefault = GetByCustomerKey(address.CustomerKey, address.AddressType).FirstOrDefault(); if (newDefault == null) return; newDefault.IsDefault = true; Save(newDefault); }
/// <summary> /// Saves customer address. /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="merchelloContext"> /// The merchello context. /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The <see cref="ICustomerAddress"/>. /// </returns> internal static ICustomerAddress SaveCustomerAddress(this ICustomer customer, IMerchelloContext merchelloContext, ICustomerAddress address) { Mandate.ParameterCondition(address.CustomerKey == customer.Key, "The customer address is not associated with this customer."); var addressList = new List<ICustomerAddress>(); // REFACTOR-v3 // Caching issue - customerAddresses can return with a null address in the list, usually caused from the // redundant cache item created by customer's being retrieved from the customer context. (Added note there). // HACK remove the null addresses here as are caused from items being removed without cache cleared correctly and // which will allow things to proceed normally (and the cache correct itself in a hacky way). var addresses = customer.Addresses.Where(x => x != null).ToList(); var isUpdate = false; foreach (var adr in addresses) { if (address.IsDefault && adr.Key != address.Key && adr.AddressType == address.AddressType) adr.IsDefault = false; if (addresses.Any(x => x.Key == address.Key)) { isUpdate = true; } addressList.Add(adr); } if (!isUpdate) addresses.Add(address); ((Customer)customer).Addresses = addresses; merchelloContext.Services.CustomerService.Save(customer); return address; }