Want to do this but as a single expression:
var maxYearVolumes = volumes.Where(v => v.YEAR == maxYear);
maxYearVolumes.Max(v => v.JAN_VOLUME);
maxYearVolumes.Max(v => v.FEB_VOLUME);
maxYearVolumes.Max(v => v.MAR_VOLUME);
maxYearVolumes.Max(v => v.APR_VOLUME);
maxYearVolumes.Max(v => v.MAY_VOLUME);
maxYearVolumes.Max(v => v.JUN_VOLUME);
maxYearVolumes.Max(v => v.JUL_VOLUME);
maxYearVolumes.Max(v => v.AUG_VOLUME);
maxYearVolumes.Max(v => v.SEP_VOLUME);
maxYearVolumes.Max(v => v.OCT_VOLUME);
maxYearVolumes.Max(v => v.NOV_VOLUME);
maxYearVolumes.Max(v => v.DEC_VOLUME);
Need a single "row" with these properties filled in with the max of each.
CodePudding user response:
As mentioned by @juharr in the comments, you can group by a constant, then take the max of each value
var maxYearVolumes = volumes
.Where(v => v.YEAR == maxYear)
.GroupBy(v => 1)
.Select(g => new {
Jan = g.Max(v => v.JAN_VOLUME),
Feb = g.Max(v => v.FEB_VOLUME),
Mar = g.Max(v => v.MAR_VOLUME),
Apr = g.Max(v => v.APR_VOLUME),
May = g.Max(v => v.MAY_VOLUME),
Jun = g.Max(v => v.JUN_VOLUME),
Jul = g.Max(v => v.JUL_VOLUME),
Aug = g.Max(v => v.AUG_VOLUME),
Sep = g.Max(v => v.SEP_VOLUME),
Oct = g.Max(v => v.OCT_VOLUME),
Nov = g.Max(v => v.NOV_VOLUME),
Dec = g.Max(v => v.DEC_VOLUME),
})
.First();
If you are using Linq-To-Objects and are worried about performance, you can use .Aggregate
instead, but it may be easier to just write a normal foreach
loop.