I have an (ugly) MS SQL LEFT JOIN
query and I would like to write it using LINQ.
select a.VersionId
FROM (SELECT root.VersionId, root.[Xml], versions.VersionId as replacedBy
FROM [Entities] as root
LEFT OUTER JOIN [Entities] as versions
on root.EntityId = versions.EntityId AND root.VersionId = versions.ReplacedVersionID
where root.EntityId = @EntityId) as a
where a.replacedBy IS NULL AND a.Xml IS NOT NULL
I know that I can use composite keys in join, and until now I came up with this:
var versionsLinq = from root in entities
join versions in entities on new { entId = versions.EntityId, replId = versions.ReplacedVersionId }
equals new { entId = root.EntityId, replId = root.VersionId }
into joinedList
from entities in joinedList.DefaultIfEmpty()
...
but this is where I get stuck. Any help would be appreciated.