I am trying to convert below SQL query to LINQ,
SELECT
TD.*, RD.*
FROM
dbo.TransactionDetail TD
INNER JOIN
dbo.Measure M ON M.InternalID = TD.MetricCode
LEFT OUTER JOIN
(SELECT
tmp.ID, tmp.ReportingDate, 1 AS Match
FROM
tmp) AS RD ON RD.ID = M.Frequency
AND RD.ReportingDate = TD.ReportingDate
WHERE
RD.Match IS NULL
AND TD.BatchID = 'e07f9855-b286-4406-9189-5cfb2a7914c8'
My Linq query looks like below,
Update : Added td and rd definition
var rd = (from tt in result
select new { ID = tt.Id, tt.ReportingDate });
// inner join
var td = TransactionDetail.Join(
MesureTb,
t => t.MetricCode,
m => m.InternalId,
(t, m) => new
{
t.Id,
t.RowAction,
t.BatchId,
t.TrustCode,
t.MetricCode,
t.ReportingDate,
t.Value,
t.UpperBenchmark,
t.LowerBenchmark,
m.InternalId,
Frequency = m.Frequency
});
var result2 = from a in td//inner join already done in previous step
join b in rd
on new { ReportingDate = (DateTime)a.ReportingDate, ID = a.Frequency } equals new { b.ReportingDate, b.ID } into j
from b in j.DefaultIfEmpty()
where b == null && a.BatchId == batchId
select a.Id;
Could someone help me how I can write this in a simple and efficient way? It is basically selecting the Non matching rows where Match = null Update : The TD in Linq contains list of reporting dates ranging from 2009 to 2021 The Right table RD is a lookup table which has date values between 2010 to 2020 Expected out put : The query should return the values from TD table which has Reporting date < 2010 and > 2020
Example below :
Expected Output : As in above screen shot, I need values which have Match as NULL.
I hope this explains what I am trying to achieve.
Any help is appreciated.
Thanks in advance.