我有以下代码:
private int AllFeb(Forecast f, IRepository repository)
{
return All(f, repository, f.Feb);
}
private int AllJan(Forecast f, IRepository repository)
{
return All(f, repository, f.Jan);
}
private int All(Forecast f, IRepository repository, int callBk)
{
var otherForecasts = repository.Query<Forecast>().Where(r => r.PersonId == f.PersonId);
if (otherForecasts.Any())
{
return otherForecasts.Sum(r => r.Feb) + callBk;
}
return 0;
}
如您所见,我正在尝试提出一个可以每个月重复使用的共享功能。问题是我需要该All
方法中的以下行:
otherForecasts.Sum(r => r.Feb)
是通用的,但我需要Sum
从外部传递方法内部的回调(因为我不希望它被硬编码为r.Feb
.
有什么办法可以避免代码重复吗?