繁体   English   中英

一次收取费用,对费用加权平均

[英]Operate on collection once, to sum weighted average of fees

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;

}  

有一些重载可以处理singlesingle? 当然要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)));

当然我无法测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM