Код: Выделить всё
IEnumerable myObjects = new List()
{new MyCoolObject(){Value1=1, Value2=10}, new MyCoolObject(){Value1=2, Value2=20}};
MyCoolObject aggregatedObject = myObjects.Aggregate(new MyCoolObject(), (accumlator, next) => new MyCoolObject()
{
Value1=accumlator.Value1 + next.Value1,
Value2=accumlator.Value2 + next.Value2
}
Другой распространенный пример:
Код: Выделить всё
MyCoolObject aggregatedObject = new MyCoolObject()
{
Value1=myObjects.Sum(x=>x.Value1),
Value2=myObjects.Sum(x=>x.Value2)
}
Я решил, что могу сделать следующее:
Код: Выделить всё
MyCoolObject aggregatedObject = myObjects.Aggregate(new MyCoolObject(), (accumlator, next) =>
{
accumlator.Value1 += next.Value1;
accumlator.Value2 += next.Value2;
return accumlator;
};
Я удивлен, что не часто вижу это решение. Есть ли какие-либо проблемы, которые может вызвать это решение и которые могли бы это объяснить?