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 8532663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:45:59+00:00 2026-06-11T09:45:59+00:00

For example, I have this class class Person{ private int _id = int.MinValue; private

  • 0

For example, I have this class

class Person{
    private int _id = int.MinValue;
    private string _name = string.Empty;
    private int _age = int.MinValue;
    private string _city = string.Empty;

    public string Id{ get { return _id ; } set { _id = value; } }
    public string Name{ get { return _name; } set { _name = value; } }
    public int Age{ get { return _age; } set { _age = value; } }
    public string City{ get { return _city ; } set { _name = city; } }
}

and a list of Person that I show in a table. In this table there is an “edit in place/inline”: some Person’s property has a cell(td) in the table, so when I edit a cell, via javascript/jquery, I create the json object with the changed value and I send it to server. The json object contains only the property changed: if i edit “Name” the json object will be:

{"obj":{"Id":"1","Name":"Anna"}}

But the object Person to the server comes as

Id = 1, Name = "Anna", Age = 0, City = null

So the problem is: to execute an update stored procedure I must create the object with all original values ​​to exceptions of the modified property. In this example, i want get this object:

Id = 1, Name = "Anna", Age = 25, City = "New York"

To create this object I use this method

public static TEntity CopyTo<TEntity>(this TEntity OriginalEntity, TEntity NewEntity){
    PropertyInfo[] oProperties = OriginalEntity.GetType().GetProperties();
    foreach (PropertyInfo CurrentProperty in oProperties.Where(p => p.CanWrite))
    {
         if (CurrentProperty.GetValue(OriginalEntity, null) == null)
         {
             CurrentProperty.SetValue(OriginalEntity, CurrentProperty.GetValue(NewEntity, null), null);
         }
    }

        return OriginalEntity;
}

If new object Person has a property with null value then I take the orginal value from the original Person (NewEntity). This way doesn’t work with number because from client to server the Age property become 0 and not null.

How I can to resolve this problem? To consider that I can not use:

  • nullable type because I should to modify so many lines of code in the whole project;
  • the table may not contain all the properties of Person, so I can’t create the entire object in javascript and then to change only the value modified.

I hope I was clear enough with my bad english

  • 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-06-11T09:46:00+00:00Added an answer on June 11, 2026 at 9:46 am

    k; if I re-phrase: you want to copy all properties that have non-null values onto an existing object. Well, the first issue (as you note) is that you can’t have an int that is null. Fortunately, you can fix that by simply taking the pragmatic step of making Age and Id a Nullable<int>, aka int?:

    class Person
    {
        public int? Id { get; set; }
        public string Name { get; set; }
        public int? Age { get; set; }
        public string City { get; set; }
    }
    

    So now we just have the “copy all non-null values”. For that, look at this existing answer, which is a bit like your reflection approach, but it uses IL to be stupidly fast.

    Example usage:

    var orig = new Person {Id = 1, Name = "Anna"};
    var delta = new Person {Id = 1, Age = 25, City = "New York"};
    
    var merged = Merger.Merge(orig, delta);
    Console.WriteLine(merged.Id);
    Console.WriteLine(merged.Age);
    Console.WriteLine(merged.Name);
    Console.WriteLine(merged.City);
    

    It could also be tweaked to do an in-place merge over one of the existing objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a class like this: class Person { private $value; public function
I have a class for example Person. public class Person { private Integer age;
For example, i have this: class BasePacket { int header; int type; } class
I have this example: public class Inheritance { public static class Animal { public
I have a class named Person with multiple properties, for example: public class Person
Take my domain class for example public class Person { private Integer id; private
I have the following classes class Person { private String name; void getName(){...}} class
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
Say I have a class ObjectA (a view controller for example), that does this
Before Consider to have a class and a global function: This is, for example,

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.