Linq自连接组合键



请问谁能帮我解决这个问题。

我有一个表需要连接到它自己。该表包含一个组合键。到目前为止,下面的SQL语句运行良好。

select * from releases as a inner join
(
select * from releases as r1
where id=50
) as x 
on (a.ParentSeriesId = x.SeriesId and a.ParentPeriod = x.Period) OR a.id=50

问题是如何将其转换为linq。

到目前为止我得出的结论是

from a in Releases
join x in (
         (from r1 in Releases
         where
         r1.Id == 50
         select new {
         r1
         }))
         on new { a.ParentSeriesId, a.ParentPeriod, a.Id }
  equals new { ParentSeriesId = x.r1.SeriesId, ParentPeriod = x.r1.Period, Id = 50 }
select new{
}

但是这会产生以下SQL语句

SELECT NULL AS [EMPTY]
FROM [Releases] AS [t0]
INNER JOIN [Releases] AS [t1] ON ([t0].[ParentSeriesId] = [t1].[SeriesId]) AND ([t0].[ParentPeriod] = [t1].[Period]) AND ([t0].[Id] = @p0)
WHERE [t1].[Id] = @p1

问题是我如何才能使它成为我原来的SQL语句。谢谢! !

Linq只支持对等连接,因为您有一个OR,请尝试以下带有两个'from'子句

var xQuery = from r in Releases
             where r.Id == 50
             select r;
var query = from r in Releases
            from x in xQuery
            where (r.ParentSeriesId == x.SeriesId && r.ParentPeriod == x.Period) ||
                   r.Id == 50  //r.Id == x.Id
            select new
            {
            }

相关内容

  • 没有找到相关文章

最新更新