一次收取,以加权平均费用相加

本文关键字:加权 一次 | 更新日期: 2023-09-27 17:55:14

return new SchoolFees(
        new Percentage(schoolFeesResult.Sum(x => (x.Amount.Value / totalFees) * x.TuitionFee.Value)),
        new Percentage(schoolFeesResult.Sum(x => (x.Amount.Value / totalFees) * x.TravellingFee.Value)),
        new Percentage(schoolFeesResult.Sum(x => (x.Amount.Value / totalFees) * x.ResidentialFee.Value)));

有没有办法对schoolFeesResult操作一次来计算每种不同类型的费用(TuitionTravellingResidence)的加权平均值。基本上我不希望(x.Amount.Value / totalFees)在我的代码中出现 3 次?

一次收取,以加权平均费用相加

你可以使用这样的东西:

var fees = from fee in schoolFeesResult
           let weight = fee.Amount.Value / totalFees
           select new 
           {
               TuitionFee = weight * fee.TuitionFee.Value,
               TravellingFee = weight * fee.TravellingFee.Value,
               ResidentialFee = weight * fee.ResidentialFee.Value
           };
// if the calculation of the fees is a performance bottleneck,
// uncomment the next line:
// fees = fees.ToList();
return new SchoolFees(
    new Percentage(fees.Sum(x => x.TuitionFee),
    new Percentage(fees.Sum(x => x.TravellingFee),
    new Percentage(fees.Sum(x => x.ResidentialFee));

你可以走得更远:

var fees = (from fee in schoolFeesResult
            let weight = fee.Amount.Value / totalFees
            group fee by 1 into g
            select new 
            {
                TuitionFee = g.Sum(x => weight * x.TuitionFee.Value),
                TravellingFee = g.Sum(x => weight * x.TravellingFee.Value),
                ResidentialFee = g.Sum(x => weight * x.ResidentialFee.Value)
            }).Single();
return new SchoolFees(
    new Percentage(fees.TuitionFee,
    new Percentage(fees.TravellingFee,
    new Percentage(fees.ResidentialFee);

但我怀疑第二个版本是否是个好主意。它使代码难以理解。我添加它纯粹是出于学术原因,以展示什么是可能的。

只是另一个令人脑残的解决方案

Func<Func<Fee, decimal>, decimal> totalFee = feeSelector =>
   schoolFeesResult.Sum(x => x.Amount.Value / totalFees * feeSelector(x));
return new SchoolFees(
   new Percentage(totalFee(f => f.TuitionFee.Value)),
   new Percentage(totalFee(f => f.TravellingFee.Value)),
   new Percentage(totalFee(f => f.ResidentialFee.Value))
);

甚至更短:

Func<Func<Fee, decimal>, Percentage> percentageOf = feeSelector =>
   new Percentage(schoolFeesResult.Sum(x => 
         x.Amount.Value / totalFees * feeSelector(x)));
return new SchoolFees(
   percentageOf(f => f.TuitionFee.Value),
   percentageOf(f => f.TravellingFee.Value),
   percentageOf(f => f.ResidentialFee.Value)
);

我使用这种WeightedAverage实现作为扩展方法来IEnumerable<T>

public static double? WeightedAverage<TSource>(this IEnumerable<TSource> source
                                                       , Func<TSource, float> weightField
                                                       , Func<TSource, double> propertyToWeight)
{
    var total = source.Sum(weightField);
    var sum = source.Select(item => weightField(item) * propertyToWeight(item)).Sum();
    return sum / total;
}  

有一些重载需要处理single,当然single?double。也许您可以调整它以适应您要实现的目标。

我想你可以把它放在另一个查询中,恕我直言,它也更可编辑:

var percentages = schoolFeesResult
    .Select(x => new { SFR = x, AmoundDivFees = (x.Amount.Value / totalFees)})
    .Select(x => new { 
        TuitionFee = x.AmoundDivFees * x.SFR.TuitionFee.Value,
        TravellingFee = x.AmoundDivFees * x.SFR.TravellingFee.Value,
        ResidentialFee = x.AmoundDivFees * x.SFR.ResidentialFee.Value
    });
return new SchoolFees(
    new Percentage(percentages.Sum(x => x.TuitionFee)),
    new Percentage(percentages.Sum(x => x.TravellingFee)),
    new Percentage(percentages.Sum(x => x.ResidentialFee)));

当然,我无法测试它。