2011-04-14 72 views
3

請有人可以幫助我這個。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語句。謝謝!!

+0

嘗試用以下兩個在LINQ查詢您的加盟條件是不完全正確的,它會之間的作用「與」條件。我的評論不是這個問題的答案,只是一個建議 – 2011-04-14 10:23:52

回答

2

的LINQ僅支持等值連接,因爲你有一個OR「從」條款

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

感謝nasmifive。我會嘗試,但我想這是唯一的方法。我會回來的任何結果。 – Byron 2011-04-14 10:52:15