Has anyone got a tidier way of doing this with linq to entities?
I am trying to get the item in each group that has the highest X, Y or Z e.g. Max( X, Y, Z )
var points = from g in groupedData
from ep in g
where (ep.X > ep.Y ?
ep.X > ep.Z ? ep.X : ep.Z
: ep.Y > ep.Z ? ep.Y : ep.Z)
== g.Max(e => e.X > e.Y ?
e.X > e.Z ? e.X : e.Z
: e.Y > e.Z ? e.Y : e.Z)
select ep;
var points = from g in groupedData
let gMax = g.Max(e => e.X > e.Y ?
(e.X > e.Z ? e.X : e.Z)
: (e.Y > e.Z ? e.Y : e.Z))
from ep in g
where ep.X == gMax
|| ep.Y == gMax
|| ep.Z == gMax
select ep;
PS : Linq2SQL or Linq2Entities ? Because you flagged "EF" !
Edit : I've just tested this with success :
var points = from g in groupedData
let gMax = g.Max(e => new int[] { e.X, e.Y, e.Z }.Max())
from ep in g
where ep.X == gMax
|| ep.Y == gMax
|| ep.Z == gMax
select ep;
Do you confirm it works in your case ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With