2009-09-14 70 views
1

JOIN我有這樣的LINQ查詢:生成充滿LinqToSQL

(from rapportBase in New_RapportReferencementBases 
join rapportExtensionAll in New_RapportReferencementExtensionBases on rapportBase.New_RapportReferencementId equals rapportExtensionAll.New_RapportReferencementId into jointureRapportExtension 
from rapportExtension in jointureRapportExtension.DefaultIfEmpty() 

join packExtensionAll in New_PackExtensionBases on rapportExtension.New_PackId equals packExtensionAll.New_PackId into jointurePackExtension 
from packExtension in jointurePackExtension.DefaultIfEmpty() 
join packBaseAll in New_PackBases on packExtension.New_PackId equals packBaseAll.New_PackId into jointurePackBase 
from packBase in jointurePackBase.DefaultIfEmpty() 

join domaineBaseAll in New_DomaineBases on packExtension.New_DomaineId equals domaineBaseAll.New_DomaineId into jointureDomaineBase 
from domaineBase in jointureDomaineBase.DefaultIfEmpty() 
join domaineExtensionAll in New_DomaineExtensionBases on domaineBase.New_DomaineId equals domaineExtensionAll.New_DomaineId into jointureDomaineExtension 
from domaineExtension in jointureDomaineExtension.DefaultIfEmpty() 

join compteBaseAll in AccountBases on domaineExtension.New_AccountId equals compteBaseAll.AccountId into jointureCompteBase 
from compteBase in jointureCompteBase.DefaultIfEmpty() 
join compteExtensionAll in AccountExtensionBases on compteBase.AccountId equals compteExtensionAll.AccountId into jointureCompteExtension 
from compteExtension in jointureCompteExtension.DefaultIfEmpty() 
select rapportBase) 

其產生:

SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode] 
FROM [New_RapportReferencementBase] AS [t0] 
LEFT OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId] 
LEFT OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId]) 
LEFT OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId] 
LEFT OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId]) 
LEFT OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId] 
LEFT OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId]) 
LEFT OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId] 

但我想生成:

SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode] 
FROM [New_RapportReferencementBase] AS [t0] 
FULL OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId] 
FULL OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId]) 
FULL OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId] 
FULL OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId]) 
FULL OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId] 
FULL OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId]) 
FULL OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId] 

換句話說,我想爲此查詢生成完整的外連接,而不僅僅是留下。

有人知道如何以簡單的方式做到這一點?

感謝

回答

1

Linq中沒有完全外部聯接。你必須做兩個左連接並將它們連接在一起。下面是一些僞代碼,看起來像LINQ:

var foj = 
    (from l in left 
    join r in right on l.Id equals r.Id into g 
    from r in g.DefaultIfEmpty() 
    select new { l, r }) 
    .Concat 
    (from r in right 
    join l in left on r.Id equals l.Id into g 
    from l in g.DefaultIfEmpty() 
    where l == null 
    select new { l, r }); 

可能會更好這樣的邏輯推到存儲過程中,如果你使用的LINQ到SQL計劃。

0

可悲的是我之前看到過,但我想確定。

我們所做的事情是在存儲過程中完成並通過linqtosql方法訪問它。

然後我們映射表的結果,就像linqtosql做的那樣,使用分組獲得enties鏈接到另一個實體的集合。

例如,如果我們有一個帳戶與一些聯繫人鏈接到它。我們要求所有聯繫人爲他們每個人提供一個完全加入的賬戶和他們的賬戶ID組,以獲得每個聯繫人的列表。

它工作得很好,但它會更好,如果linqtosql能夠生成完全加入....