Kaushal Kaushal - 3 months ago 9
SQL Question

LINQ join on non equal keys

I need to convert this SQL query into LINQ

SELECT *
FROM Table1 t1
JOIN Table2 t2 ON t1.LogDate >= t2.StartDate AND t1.LogDate <= t2.EndDate


What should be the LINQ for above SQL query?

Answer

LINQ only supports join on equal keys, I suggest using cross join. Depending on your query optimizer it may be converted to inner join

from t1 in Table1
from t2 in Table2
where t1.LogDate >= t2.StartDate && t1.LogDate <= t2.EndDate
select new { t1, t2 }
Comments