Switch between aggregate functions with linq?

Try the following public IEnumerable DoStuff( IEnumerable someItems, Func,Item> reduce) { var items = someItems. GroupBy(i => i. Date).

Select(reduce); ... } DoStuff(someItems, p => p. Sum(r => r. Value)); DoStuff(someItems, p => p.

Max(r => r. Value)).

The addition of the selector inside is a pain, but ultimately you could take a Func,decimal> , and pass in Enumerable. Max or Enumerable. Sum as the delegate instance.To do this without duplicating the =>r.

Value selector would require doing the selector first, i.e. // usage: DoStuff(items, Enumerable. Sum); DoStuff(items, Enumerable.

Max); // shared method: public IEnumerable DoStuff(IEnumerable someItems, Func,decimal> aggregate) { var items = someItems. GroupBy(i => i. Date).

Select( p => new Item(p. Key, aggregate(p. Select(r=>r.

Value)))); ... }.

Yikes! : public IEnumerable DoOtherStuff(IEnumerable someItems, Func, Func, decimal> > aggregateForGrouping ) { var items = someItems. GroupBy(i => i.

Date) . Select(p => new Item(p. Key, aggregateForGrouping(p)(r => r.

Value))); // ... } DoOtherStuff(someItems, p => p. Max); Ummm, don't do this, do what JaredPar said... Or if you still want to get this syntax, use some serious aliasing. Using ItemGroupingByDate = IGrouping; using AggregateItems = Func, decimal>; public IEnumerable DoOtherStuff( IEnumerable someItems, Func getAggregateForGrouping ) { var items = someItems.

GroupBy(i => i. Date) . Select(p => new Item(p.

Key, getAggregateForGrouping(p)(r => r. Value))); // ... } It might almost be readable if you could use aliases in other aliases, or match custom delegates with matching Func signatures, then you could jam "Selector" in there instead of "Func". We're still way down a rabbit hole, though, so this is not the solution you are looking for.

Just put here for demonstration purposes :).

1 - Altough I liked, the method signature becomes too hard to read, even with the aliases. – Fernando Aug 18 '10 at 11:57.

Key, aggregateForGrouping(p)(r => r. DoOtherStuff(someItems, p => p. Or if you still want to get this syntax, use some serious aliasing.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions