LINQ query with multiple aggregates?

You can't efficiently select multiple aggregates in vanilla LINQ to Objects. You can perform multiple queries, of course, but that may well be inefficient depending on your data source I have a framework which copes with this which I call "Push LINQ" - it's only a hobby (for me and Marc Gravell) but we believe it works pretty well. It's available as part of MiscUtil and you can read about it in my blog post on it It looks slightly odd - because you define where you want the results to go as "futures", then push the data through the query, then retrieve the results - but once you get your head round it, it's fine.

I'd be interested to hear how you get on with it - if you use it, please mail me at skeet@pobox.com.

You can't efficiently select multiple aggregates in vanilla LINQ to Objects. You can perform multiple queries, of course, but that may well be inefficient depending on your data source. I have a framework which copes with this which I call "Push LINQ" - it's only a hobby (for me and Marc Gravell) but we believe it works pretty well.It's available as part of MiscUtil, and you can read about it in my blog post on it.

It looks slightly odd - because you define where you want the results to go as "futures", then push the data through the query, then retrieve the results - but once you get your head round it, it's fine. I'd be interested to hear how you get on with it - if you use it, please mail me at skeet@pobox.com.

Thanks Jon. One of the hardest parts about Linq so far is figuring out what you can and can't do. I'll definitely take a look at your Push technique once I get a bit more comfortable with the standard methodology.

– Darrel Miller Sep 30 '08 at 20:07.

Single enumeration yielding both min and max (and any other aggregate you want to throw in there). This is much easier in vb.net. I know this doesn't handle the empty case.

That's pretty easy to add. List myInts = new List() { 1, 4, 2, 0, 3 }; var y = myInts. Aggregate( new { Min = int.

MaxValue, Max = int. MinValue }, (a, i) => new { Min = (i I : a. Min, Max = (a.

Max I : a. Max }); Console. WriteLine("{0} {1}", y.

Min, y. Max).

It is a bit manual but it is good enough for me. It is funny how when you break linq down you quickly return to just syntactic shortcuts for a foreach look. – Darrel Miller Sep 30 '08 at 21:54 Ironic then, isn't it?

Foreach is just a syntactic shortcut. – David B Sep 30 '08 at 23:39.

It is possible to do multiple aggregates with LINQ-to-Objects, but it is a little ugly. Var times = punches. Aggregate( new { EarliestIn = default(DateTime?), LatestOut = default(DateTime?

) }, (agg, p) => new { EarliestIn = Min( agg. EarliestIn, p. Type == "In"?

(DateTime? )p. PunchTime : default(DateTime?

)), LatestOut = Max( agg. LatestOut, p. Type == "Out"?

(DateTime? )p. PunchTime : default(DateTime?

)) } ); You would also need Min and Max functions for DateTime since these are not available standard. Public static DateTime? Max(DateTime?

D1, DateTime? D2) { if (!d1. HasValue) return d2; if (!d2.

HasValue) return d1; return d1. Value > d2. Value?

D1 : d2; } public static DateTime? Min(DateTime? D1, DateTime?

D2) { if (!d1. HasValue) return d2; if (!d2. HasValue) return d1; return d1.

Value D1 : d2; }.

You can't efficiently select multiple aggregates in vanilla LINQ to Objects. You can perform multiple queries, of course, but that may well be inefficient depending on your data source. I have a framework which copes with this which I call "Push LINQ" - it's only a hobby (for me and Marc Gravell) but we believe it works pretty well.

It's available as part of MiscUtil, and you can read about it in my blog post on it. It looks slightly odd - because you define where you want the results to go as "futures", then push the data through the query, then retrieve the results - but once you get your head round it, it's fine. I'd be interested to hear how you get on with it - if you use it, please mail me at skeet@pobox.com.

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