开发者

Linq self join composite key

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].[SeriesI开发者_StackOverflowd]) 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!!


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
            {

            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜