我有一个简单的对象:
public class Machine
{
public string Name { get; set; }
public int Power { get; set; }
public int Type { get; set; }
}
然后是一个类,其中包含这些对象的列表:
public class Aggregations
{
private List<Machine> _machines;
public Aggregations()
{
_machines = new List<Machine>
{
new Machine { Name = "XLR1", Power = 111, Type = 1 },
new Machine { Name = "XLR2", Power = 222, Type = 1 },
new Machine { Name = "XLR3", Power = 333, Type = 1 },
new Machine { Name = "XLR4", Power = 444, Type = 1 },
new Machine { Name = "XLR5", Power = 555, Type = 2 },
new Machine { Name = "XLR6", Power = 666, Type = 2 }
};
}
// ...
}
有两个函数可返回具有特定条件的计算机列表:
public IEnumerable<Machine> MaxPower(IEnumerable<Machine> machines)
{
var maxPowerMachinesPerType = new List<Machine>();
var groups = machines.GroupBy(m => m.Type);
foreach (var g in groups)
{
var max = g.Max(m => m.Power);
var machine = g.First(m => m.Power == max);
maxPowerMachinesPerType.Add(machine);
}
return maxPowerMachinesPerType;
}
public IEnumerable<Machine> MinPower(IEnumerable<Machine> machines)
{
var minPowerMachinesPerType = new List<Machine>();
var groups = machines.GroupBy(m => m.Type);
foreach (var g in groups)
{
var min = g.Min(m => m.Power);
var machine = g.First(m => m.Power == min);
minPowerMachinesPerType.Add(machine);
}
return minPowerMachinesPerType;
}
}
如您所见,这两个函数几乎相等.仅“最大”和“最小”不同.
这些函数的调用方式如下:
IEnumerable<Machine> maxPowerMachines = MaxPower(_machines);
IEnumerable<Machine> minPowerMachines = MinPower(_machines);
因为我的实际程序稍微复杂些,尽管我想调用其他聚合函数,所以我想传递聚合函数用作参数:(伪代码)
IEnumerable<Machine> maxPowerMachines = SuperFunction(_machines, m => m.Max);
IEnumerable<Machine> minPowerMachines = SuperFunction(_machines, m => m.Min);
IEnumerable<Machine> averagePowerMachines = SuperFunction(_machines, m => m.Average);
我希望你能得到意图.
解决方法:
由于Min和Max具有相同的签名,即它们都采用IEnumerable< T>.并产生一个T,您可以这样:
public IEnumerable<Machine> SelectPower(
IEnumerable<Machine> machines
, Func<IEnumerable<int>,int> powerSelector
) {
var res = new List<Machine>();
var groups = machines.GroupBy(m => m.Type);
foreach (var g in groups) {
var targetPower = powerSelector(g.Select(m => m.Power));
var machine = g.First(m => m.Power == targetPower);
res.Add(machine);
}
return res;
}
现在您可以像这样调用方法:
IEnumerable<Machine> maxPowerMachines = SuperFunction(_machines, m => m.Max());
IEnumerable<Machine> minPowerMachines = SuperFunction(_machines, m => m.Min());