I think the title speaks for itself. I want to create a LINQ query with aggregations on multiple columns where no grouping is applied. This is just something that can be done very simple in plain SQL.
1 SELECT 2 MAX(HorsePower), 3 AVG(Weight) 4 FROM 5 Car
But when you want to create a construction that's similar in LINQ you will get an compilation error, because of a wrong syntax.
1 var result = from car in sut.Cars 2 select new 3 { 4 MaxHorsePower = car.Max(p => p.HorsePower), 5 AverageWeight = car.Average(p => p.Weight) 6 };
The trouble is that this doesn't work. You can do an aggregation on an complete set, like the following. This will result in just two queries.
1 var result2 = (from car in sut.Cars select car.HorsePower).Max(); 2 var result3 = (from car in sut.Cars select car.Weight).Average();
You can actually do multiple aggregations at once when using groups. The sad thing is, we don't always have something to group on. But that can be solved.
1 var result = (from car in 2 (from car in sut.Cars 3 select new 4 { 5 car.HorsePower, 6 car.Weight, 7 Dummy = string.Empty 8 }) 9 group car by new {car.Dummy} 10 into dummyGroup 11 select new 12 { 13 MaxHorsePower = dummyGroup.Max(p => p.HorsePower), 14 AverageWeight = dummyGroup.Average(p => p.Weight) 15 }).Single();
This solution adds a dummy column called 'Dummy' with the same value for every record. Because this value is the same for every record we can safely group on it and expect to have only one group. This solution makes sure we have one query for the database, but with the use of a workaround.
I would say yes this works, but for readability I would not suggest the use of this workaround for the trivial problem above. By using two queries we have a very manageable solution that's readable from code. The workaround needs at least some comments before we can totally understand it as someone new to the workaround.
But it in the end we can also say, we now know how we can fake the grouping. Use it with care, as I mentioned because of the manageability of your code.
I actually like what you've acquired here, certainly
like what you're stating and the way in which you say it.
You make it enjoyable and you still take care of to keep it wise.
I can't wait to read far more from you. This is actually a tremendous web
site.
without doubt obtain pleɑsant knowledge.
in touch more approximately your article on AOL? I require a
specialist on this area to unravel my problem. Maybe that's you!
Looking forward to look you.
not mind. I was interested to find out how you center yourself and clear your head prior to writing.
I've had trouble clearing my mind in getting my thoughts out.
I do enjoy writing but it just seems like the first 10 to
15 minutes are lost simply just trying to figure
out how to begin. Any suggestions or tips? Thanks!
I will book mark your blog and keep checking for new details about once a week.
I opted in for your Feed as well.
is for sale pay attention to how long it has been up for sale, and pay
particular attention to what is selling fast. Using Google on research about
where to buy cheap wow gold, below is an overview of the top 5 results of sites offering Wo - W gold services from the search where to buy
cheap Wo - W gold:.
vaccine was developed.
http://www.policesunglasses.us.com
http://www.cartieroutlet.us.com
http://www.cazal.us.com
http://www.chromehearts-outlet.comg drtg
http://www.chanelsunglasses.in.net
http://www.burberrysunglasses.us.com
http://www.versacesunglasses.us.com
http://www.cartierwatches.us.com
http://www.chromeheartsoutlet.in.net
http://www.pradasunglasses.us.com
http://www.dior.us.com
http://www.cartier.us.com
http://www.guccisunglasses.us.com
http://www.louisvuittonsunglasses.us.com
Watch Toto en zijne zuster te Brussel ( 2000 ) Online HD 1080p http://maiksmovie.com/toto-en-zijne-zuster-te-brussel- (-2000-)_279527/
teeth whitening.
If so howw do you protect against it, any plugin or
anything you can suggest? I get so much lately it's driving me insane
so any support is very much appreciated.
see this web site daily as it ofdfers feature contents, thanks
it and personally recommend to my friends. I am sure they will be benefited froim this web site.
I really found you by mistake, while I was researching on Google
for something else, Anyhow I am here noww and would just lime to say thanks for
a maarvelous post andd a alll round thrilling blog (I
also love the theme/design), I don't have time to rewad through it all at the moment buut I have book-marked it and also added your
RSS feeds, so when I have time I will be back to read much
more, Please do keep up the superb work.
<a href= http://de-edapotheke.com/blog/wp-content/uploads/2016/05/google666.jpg>
<IMG> http://de-edapotheke.com/blog/wp-content/uploads/2016/05/google666.jpg</IMG>
</a>
<a href= http://de-edapotheke.com/blog/wp-content/uploads/2016/05/google666.jpg>
<IMG> http://de-edapotheke.com/blog/wp-content/uploads/2016/05/google666.jpg</IMG>
</a>
Mocht jij gaan verhuizen is 't van belang dat dit zorgvuldig geschied,
ten slotte draait dit omwille jouw bruikbare rommel. Wij raden daarom ook aan om een onderneming
te vinden die zorgvuldig gecertificeerd zijn.
Één dergelijke onderneming kun jij evenzo identificeren met
hun referenties. Wellicht heb je deze certificaten wel is eerder voorbij zien komen, toch vaak zie je deze op
verhuiswagens bij desbetreffende verhuisfirma's.
Kwaliteit is vaak ook te onderscheiden aan de certificaten, jij weet bij 't selecteren van zo één onderneming dat ze het waar hebben kunnen maken. We raden aan dan evenzo ten alle
tijden te kijken voor organisaties die een keurmerk dragen.
on Digg for something else, Nonetheless I
am here now and would just like to say thanks a lot for a tremendous post
and a all round enjoyable blog (I also love the theme/design), I don't have time to go through it all at the minute but I have saved it and also
added your RSS feeds, so when I have time I will be back
to read much more, Please do keep up the fantastic job.