Sql Linq自连接复合密钥
请有人帮我做这个 我有一张桌子需要自己连接起来。该表包含一个复合键。到目前为止,下面的SQL语句工作正常Sql Linq自连接复合密钥,sql,linq,key,composite,Sql,Linq,Key,Composite,请有人帮我做这个 我有一张桌子需要自己连接起来。该表包含一个复合键。到目前为止,下面的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 到目前为止,我得出的结论是 f
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只支持Equijoin,因为您有一个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
{
}
在linq查询中,您的连接条件不太正确,它将在条件之间产生“AND”的效果。我的评论不是对这个问题的回答,只是一个建议谢谢你。我会试试的,但我想这是唯一的办法。我会带着任何结果回来的。