1
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一次来计算每种不同类型费用的加权平均值(Tuition, Travelling, Residence)。基本上我不想(x.Amount.Value / totalFees)在我的代码中出现 3 次?

4

4 回答 4

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);

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

于 2013-01-22T12:44:01.137 回答
2

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

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)
);
于 2013-01-22T13:16:17.303 回答
1

我将此实现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。也许你可以调整它以适应你想要实现的目标。

于 2013-01-22T12:35:01.753 回答
1

我假设你可以把它放在另一个查询中,恕我直言,它也更可红色:

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)));

我当然无法测试它。

于 2013-01-22T12:37:52.573 回答