I'm working with C# .NET Framework and I need to know how can I validate all fields that I need to update, for example, I have a table with four fields, so, if the user only have to change a field, how can I update only that field, actually I have this code:
public async Task<ServiceResponse<User>> UpdateUser(User newUserUpdate)
{
ServiceResponse<User> serviceResponse = new();
//Este llamada es para buscar por medio del ID ingresado por el usuario
User userFinded = await _dataContext.Users.FindAsync(newUserUpdate.Id);
//¿Como iterar para validar que campos actualizar?
if (newUserUpdate.userName != null) userFinded.userName = newUserUpdate.userName;
if (newUserUpdate.email != null) userFinded.email = newUserUpdate.email;
if (newUserUpdate.password != null) userFinded.password = newUserUpdate.password;
_dataContext.Users.Update(userFinded);
_dataContext.SaveChanges();
serviceResponse.Message = "Usuario Actualizado Exitosamente";
return serviceResponse;
}
I´m using IF sentence to validate and update the new value, but I think that is not a good practice. Maybe is a lambda expression?