I have two methods that look almost the same, except for the aggregate function used in the Linq query. For example:
public IEnumerable<Item> DoStuff(IEnumerable<Item> someItems) {
var items = someItems.GroupBy(i => i.Date).Select(p => new Item(p.Key, p.Sum(r => r.Value)));
// ...
}
public IEnumerable<Item> DoOtherStuff(IEnumerable<Item> someItems) {
var items = someItems.GroupBy(i => i.Date).Select(p => new Item(p.Key, p.Max(r => r.Value)));
// ...
}
where the Item
is a class like this:
public class Item {
public DateTime Date { get; private set; }
public decimal Value { get; private set; }
public Item(DateTime date, decimal value) {
Date = date;
Value = value;
}
}
Since both methods do the same thing, I would like have just one method where I pass the IEnumerable and the aggregate function (sum / max) as parameters. How can I achieve that?