Assuming I have an object class MyObject with the following properties:
class MyObject {
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
public int MyProperty3 { get; set; }
}
And I have an array of MyObject[] with the following elements:
MyObject[] myObjects => new MyObject[] { myObject1, myObject2, myObject3 };
How do I create a new instance myObject such that its MyProperty1, MyProperty2, and MyProperty3 are the sums of the respective properties for every such object in the array?
Currently, my implementation is as follows
MyObject MyObjectSummed => new MyObject()
{
MyProperty1 = myObjects.Sum(x => x.MyProperty1);
MyProperty2 = myObjects.Sum(x => x.MyProperty2);
MyProperty3 = myObjects.Sum(x => x.MyProperty3);
}
but I vaguely remember seeing a more efficient way of doing this using LINQ, using a single line of code.
Is this possible and can someone please point me in the right direction?
Thanks!