Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7578255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:23:20+00:00 2026-05-30T17:23:20+00:00

So, i have this object for exemple: public class User { public int Id

  • 0

So, i have this object for exemple:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<Car> Cars { get; set; }
    public List<User> Children { get; set; }
}

public class Car
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Color { get; set; }
}

So, there is a way to update a User object, updating it Children and Cars?
If there isn’t a car in the object to update that there is in the new object, add it, but if there is in the object to update and there isn’t in the new object, remove it from the object to update, and update all attributes for all cars that are matched, and also to the Childrenproperty.

Is it possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:23:21+00:00Added an answer on May 30, 2026 at 5:23 pm

    Yes! It’s possible, here the solution (this solution update all properties, class properties and collection properties, but if there is an class inside, the class needs to inherit of an AbstractEntity):

    private void UpdateAllProperties<idType, entityType>(entityType currentEntity, entityType newEntity)
        where idType : IEquatable<idType>
        where entityType : AbstractEntity<idType>
    {
        var currentEntityProperties = currentEntity.GetType().GetProperties();
        var newEntityProperties = newEntity.GetType().GetProperties();
    
        foreach (var currentEntityProperty in currentEntityProperties)
        {
            foreach (var newEntityProperty in newEntityProperties)
            {
                if (newEntityProperty.Name == currentEntityProperty.Name)
                {
                    if (currentEntityProperty.PropertyType.BaseType.IsGenericType &&
                        currentEntityProperty.PropertyType.BaseType.GetGenericTypeDefinition() == typeof(AbstractEntity<>))
                    {
                        var idPropertyType = currentEntityProperty.PropertyType.GetProperty("Id").PropertyType;
                        var entityPropertyType = currentEntityProperty.PropertyType;
    
                        this.InvokeUpdateAllProperties(currentEntityProperty.GetValue(currentEntity, null),
                                                        newEntityProperty.GetValue(newEntity, null),
                                                        idPropertyType, entityPropertyType);
    
                        break;
                    }
                    else if (currentEntityProperty.PropertyType.GetInterfaces().Any(
                                x => x.IsGenericType &&
                                        x.GetGenericTypeDefinition() == typeof(ICollection<>)))
                    {
                        dynamic currentCollection = currentEntityProperty.GetValue(currentEntity, null);
                        dynamic newCollection = newEntityProperty.GetValue(newEntity, null);
    
                        this.UpdateCollectionItems(currentEntityProperty, currentCollection, newCollection);
    
                        dynamic itemsToRemove = Enumerable.ToList(Enumerable.Except(currentCollection, newCollection));
                        dynamic itemsToAdd = Enumerable.ToList(Enumerable.Except(newCollection, currentCollection));
                        dynamic itemsAreEqual = Enumerable.ToList(Enumerable.Intersect(currentCollection, newCollection));
    
                        for (int i = 0; i < itemsToRemove.Count; i++)
                        {
                            currentCollection.Remove(Enumerable.ElementAt(itemsToRemove, i));
                        }
    
                        for (int i = 0; i < itemsToAdd.Count; i++)
                        {
                            currentCollection.Add(Enumerable.ElementAt(itemsToAdd, i));
                        }
    
                        break;
                    }
                    else
                    {
                        currentEntityProperty.SetValue(currentEntity, newEntityProperty.GetValue(newEntity, null), null);
    
                        break;
                    }
                }
            }
        }
    }
    
    private void UpdateCollectionItems(PropertyInfo currentEntityProperty, dynamic currentCollection, dynamic newCollection)
    {
        var collectionType = currentEntityProperty.PropertyType.GetInterfaces().Where(
                                x => x.IsGenericType &&
                                        x.GetGenericTypeDefinition() == typeof(ICollection<>)).First();
    
        var argumentType = collectionType.GetGenericArguments()[0];
    
        if (argumentType.BaseType.IsGenericType &&
            argumentType.BaseType.GetGenericTypeDefinition() == typeof(AbstractEntity<>))
        {
            foreach (var currentItem in currentCollection)
            {
                foreach (var newItem in newCollection)
                {
                    if (currentItem.Equals(newItem))
                    {
                        var idPropertyType = currentItem.GetType().GetProperty("Id").PropertyType;
                        var entityPropertyType = currentItem.GetType();
    
                        this.InvokeUpdateAllProperties(currentItem, newItem, idPropertyType, entityPropertyType);
                    }
                }
            }
        }
    }
    
    private void InvokeUpdateAllProperties(dynamic currentEntity, dynamic newEntity, dynamic idPropertyType, dynamic entityPropertyType)
    {
        var method = this.GetType().GetMethod("UpdateAllProperties", BindingFlags.Instance | BindingFlags.NonPublic);
        var genericMethod = method.MakeGenericMethod(idPropertyType, entityPropertyType);
        genericMethod.Invoke(this, new[] { currentEntity, newEntity });
    }
    

    An exemple of the usage:

    The AbstractEntity:

    public abstract class AbstractEntity<idType>
        where idType : IEquatable<idType>
    {
        public idType Id { get; set; }
    }
    

    The exemple classes:

    public class User : AbstractEntity<int>
    {
        public string Name { get; set; }
        public List<Car> OtherCars { get; set; }
        public Car MainCar { get; set; }
    
        public bool Equals(Car other)
        {
            if (this.Id == other.Id)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    
    public class Car : AbstractEntity<int>
    {
        public string Name { get; set; }
        public string Color { get; set; }
    
        public bool Equals(Car other)
        {
            if (this.Id == other.Id)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    

    Using the method:

    User currentUser = new User()
    {
        Id = 1,
        Name = "Vinicius",
        OtherCars = new List<Car>()
        {
            new Car()
            {
                Id = 2,
                Name = "Corsa II",
                Color = "Azul"
            },
            new Car()
            {
                Id = 3,
                Name = "Palio",
                Color = "Vermelho"
            },
            new Car()
            {
                Id = 4,
                Name = "Fusca",
                Color = "Azul"
            }
        },
        MainCar = new Car()
        {
            Id = 1,
            Name = "Corsa",
            Color = "Preto"
        }
    };
    
    User updatedUser = new User()
    {
        Id = 1,
        Name = "Vinicius Ottoni",
        OtherCars = new List<Car>()
        {
            new Car()
            {
                Id = 5,
                Name = "Voyage",
                Color = "Azul"
            },
            new Car()
            {
                Id = 6,
                Name = "Voyage II",
                Color = "Vermelho"
            },
            new Car()
            {
                Id = 4,
                Name = "Fusca",
                Color = "Rosa"
            }
        },
        MainCar = new Car()
        {
            Id = 2,
            Name = "Voyage",
            Color = "Vinho"
        }
    };
    
    this.UpdateAllProperties<int, User>(currentUser, updatedUser);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have this domain object... public class SpansMultipleTables { public int CommonID {get;
I have this object: class a { public string Application; public DateTime From, To;
I have a object User and it is the following class: public class User
If I have for example one class like public class User{ public int Id
Let's say I have this code: class Score { public Update(int score) { update
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have this interface: public interface IValidationCRUD { public ICRUDValidation IsValid(object obj); private void
I have this JSON string: { success:true,user_id:309,id:309,sessId:false,email:null,name:Mai Van Quan,username:quanmv,role:Reseller Admin,messages:,org_name:null,microPayNumber:4949,microPayWord:neocam,mobile:null,permissions:{ADD_CAMERA:true, REMOVE_CAMERA:true, EDIT_CAM_GENERAL:true, ACCESS_CAM_TECHNICAL:true, EDIT_CAM_PKG:true,
private List<T> newList; public List<T> NewList { get{return newList;} set{newList = value;} } I
I have this object graph, I want to map: abstract Account (username, password, ...)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.