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 语句。谢谢!!
please somebody could help me with this one.
I have a table that needs to be joined to itself. The table contains a composite key. So far the following SQL statement works perferct.
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
The problem is how to translate this to linq.
What I have come out with so far is
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{
}
But this produces the following SQL statement
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
The problem is how can I manage to make it as my original SQL statement. Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Linq 仅支持等值连接,因为您有一个 OR,请尝试使用两个“from”子句执行以下操作
Linq only supports equijoins, since you have an OR try the following with two 'from' clauses