【发布时间】:2025-11-29 20:55:02
【问题描述】:
我有下一个非常未优化的代码:
void Summarize(IEnumerable<Section> sections)
{
this.DateBegin = sections.Select(s => s.Date).Min();
this.DateEnd = sections.Select(s => s.Date).Max();
this.Income = sections.Where(s => s.IsIncome).Sum(r => r.Amount);
this.ExpenditureBank = sections.Where(s => s.IsExpenditureBank).Sum(r => r.Amount);
this.ExpenditureClient = sections.Where(s => s.IsExpenditureClient).Sum(r => r.Amount);
this.Expenditure = this.ExpenditureBank + this.ExpenditureClient;
}
如果适用,如何使用IEnumerable.Aggregate() 重写它?
【问题讨论】:
标签: c# .net linq ienumerable aggregate