I have this pretty easy SQL Statement and I want to get the same data using LINQ, but I can't seem to find the right way.
select A.AUFTRAGID,
A.AUFTRAGNR,
A.GESELLSCHAFTID,
A.DEBITORID,
A.DEBITOR_KOMMNR2,
A.DEBITOR_TEILELIEFERNR,
D.DEPOTID,
D.DEPOTNUM,
D.MATCHCODE,
D.NAME,
D.KS_ID
from AUFTRAG A
join DEPOT D on D.DEPOTID = A.DEPOTID and D.VALID = 1 and D.KS_ID = 1
where A.AUFTRAGID in
(select AUFTRAGID from AUFTRAG_STATUS where VALID = 1
and CRTI = (select max(CRTI) from AUFTRAG_STATUS where AUFTRAGID = A.AUFTRAGID)
and [STATUS] = 9)
I can successfully join the tables [AUFTRAG] and [DEPOT], but when it comes to the last where clause I am not able to figure out how to get my data in LINQ.
I'm looking forward for your help.
UPDATE - This is what I have done so far:
var erfAuftr = (from auf in db.AUFTRAG
join dep in (from dep in db.DEPOT
where dep.KS_ID == 1
select dep) on auf.DEPOTID equals dep.DEPOTID
join a_s in
(from a_s in db.AUFTRAG_STATUS
group a_s by new
{
a_s.AUFTRAGID
} into grp
select new
{
AuftragId = grp.Key.AUFTRAGID,
Date = grp.Max(s => s.CRTI)
}) on auf.AUFTRAGID equals a_s.AuftragId
select new
{
AuftragId = auf.AUFTRAGID,
AuftragNr = auf.AUFTRAGNR,
DebitorId = auf.DEBITORID,
KVNr = auf.DEBITOR_KOMMNR2,
TL = auf.DEBITOR_TEILELIEFERNR,
DepotId = dep.DEPOTID,
DepotNum = dep.DEPOTNUM,
DepotMatchcode = dep.MATCHCODE,
DepName = dep.NAME1,
WEDate = a_s.Date
});
But I am still missing how to check for [STATUS] = 9
I finally got it working by using the following expression:
Date = grp.Where(g => g.CRTI == grp.Max(s => s.CRTI) && g.STATUS == 9).Max(s => s.CRTI)
Thank you for your help anyway!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments