If you want do it with linq you can use
Expression Another answers provide details and examples for this way. But If you want dynamically get all properties with in type -The simply way is Reflection
Note that Reflection hits by performance and usually we should use this ways only if another approach is impossible. That is why i do not suggest use reflection. Instead of it i provide example with helper method inside your class
class Model
{
private Dictionary<string, int> fieldsForSum = new Dictionary<string, int>();
private int age;
private int year;
public int Age
{
get { return age; }
set
{
bool isContain = fieldsForSum.ContainsKey(nameof(age));
if (isContain)
fieldsForSum[nameof(age)] = value;
else
fieldsForSum.Add(nameof(age), value);
age = value;
}
}
public int Year
{
get { return year; }
set
{
bool isContain = fieldsForSum.ContainsKey(nameof(year));
if (isContain)
fieldsForSum[nameof(year)] = value;
else
fieldsForSum.Add(nameof(year), value);
year = value;
}
}
public int CalcIntFields()
{
return fieldsForSum.Values.Sum();
}
}
and example of usage
var m = new Model
{
Age = 4,
Year = 3
};
var sum = m.CalcIntFields();//7
m.Year = 1;
sum = m.CalcIntFields(); //5