I am trying to convert a SQL query into c# equivalent LINQ/Lambda like below
SELECT mv.MeasureID, mv.ProviderID, MAX(mv.ReportingDate) AS mostRecentReportingDate
FROM (((TransactionDetail AS td
INNER JOIN #provider AS p ON td.TrustCode = p.Code)
INNER JOIN #measure AS m ON td.MetricCode = m.InternalID)
INNER JOIN #measureValue AS mv ON m.ID = mv.MeasureID AND p.ID = mv.ProviderID)
WHERE td.BatchID = @batchID AND
td.RowAction = 'A' AND
(m.Type = 7 OR m.Type = 8) AND
td.Value <> mv.Value
GROUP BY mv.MeasureID, mv.ProviderID
I am stuck at this line
INNER JOIN #measureValue AS mv ON m.ID = mv.MeasureID AND p.ID = mv.ProviderID)
Below is what I have tried so far
var total = (from TD in ingestionHubContext.TransactionDetail
join P in provider on TD.TrustCode equals P.Code
join M in measure on TD.MetricCode equals M.InternalId
join MV in measureValue on M.Id equals MV.MeasureId //and logic to be fixed
where TD.BatchId == batchId && TD.RowAction == "A"
&& (M.Type == 7 || M.Type == 8) && TD.Value != MV.Value
group TD by new { MV.MeasureId, MV.ProviderId } into Total
select Total);
Could you please suggest how I can write this in a better way? and handle the JOIN with AND condition in the 4th line of my code.
I have tried below which does not work since they need to match (errors at P.Id)
join MV in measureValue
on new { M.id, P.Id } equals new { MV.MeasureId, MV.ProviderId }
I also need help in doing the MAX(mv.ReportingDate)
Any help is much appreciated. Thanks in advance.