Linq 自连接复合键

发布于 2024-11-01 10:00:07 字数 933 浏览 0 评论 0原文

请有人能帮我解决这个问题。

我有一张需要连接到自身的桌子。该表包含一个复合键。到目前为止,以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2024-11-08 10:00:07

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
            {

            }

Linq only supports equijoins, since you have an OR try the following with two 'from' clauses

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
            {

            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文