gatolindo (1) [Avatar] Offline
#1
hi. i have this bit of code that I need to convert into LINQ. I've been going on it for days and so far, I didnt get it to work. Any ideas?? Tks in advance.
GatoLindo

SELECT dkmId
FROM Dierkenmerken
WHERE (gbrId = @gbrId) AND (dkmTijdstip =
(SELECT MAX(dkmTijdstip) AS Expr1
FROM Dierkenmerken AS Dierkenmerken_1
WHERE (gbrId = @gbrId)))
violentk (3) [Avatar] Offline
#2
Re: SQL to linq
Erm, maybe that SQL statement is confusing, simplify that SQL statement first such that LINQ to SQL can recognise.

Whenever i see "value = MAX(value)", reminds me of the cookie/chocolate chip solution in chapter 4 still fresh on my mind altho thats for Linq to Object smilie

The objective is clear.
U have a Box of Cookies (diekenMerken) ,
there's cookies (DKMLD) of different shape (GDRID) ,
every cookies have chocolate chips (DKMTIJDSTIP).
U want to ignore all shapes except Square and find the cookies with the most chocolate chips among the Squared Cookies


SELECT A.DKMLD
from DiekenMerken A INNER JOIN
(
SELECT MAX(DKMTIJDSTIP) as MAXNUMBER, GDRLD
from DIEKENMERKEN
Group By GDRLD
) B
on A.GDRLD = B.GDRLD and A.dkmtijdstip = B.maxnumber
where A.GDRLD = 'SQUARE'


Now convert this to LINQ...

not sure if the below helps the efficiency.

SELECT A.DKMLD
from DiekenMerken A INNER JOIN
(
SELECT MAX(DKMTIJDSTIP) as MAXNUMBER, GDRLD
from DIEKENMERKEN where A.GDRLD = 'SQUARE'
Group By GDRLD
) B
on A.GDRLD = B.GDRLD and A.dkmtijdstip = B.maxnumber

/* OR */

SELECT A.DKMLD
from DiekenMerken A INNER JOIN
(
SELECT MAX(DKMTIJDSTIP) as MAXNUMBER
from DIEKENMERKEN where A.GDRLD = 'SQUARE'
) B
on A.dkmtijdstip = B.maxnumber
where A.GDRLD = 'SQUARE'