使用RIGHT JOIN进行SQL查询的C#LINQ转换

时间:2016-04-07 16:18:17

标签: c# sql linq

我在ACCESS中有一个查询,我必须在C#LINQ中翻译这个查询。

SELECT DETAIL.*, 
       "" AS DBC_ART, 
       A_ENS.ENS_CODE, 
       A_LEA.LEA_ART, 
       A_ART_1.ART_CHA, 
       A_ART_1.ART_ANA, 
       A_ANA_REPART.ANA_CLA2 
FROM (
       (A_LEA RIGHT JOIN 
        ((DETAIL LEFT JOIN A_ART ON DETAIL.DFA_ART = A_ART.ART_CODE)
         LEFT JOIN A_ENS ON DETAIL.DFA_ART = A_ENS.ENS_CODE) ON A_LEA.LEA_ENS = A_ENS.ENS_CODE) 
         LEFT JOIN A_ART AS A_ART_1 ON A_LEA.LEA_ART = A_ART_1.ART_CODE) 
         LEFT JOIN A_ANA_REPART ON A_ART_1.ART_ANA = A_ANA_REPART.ANA_CODE 
WHERE (((A_ENS.ENS_CODE) Is Not Null) AND ((A_ART.ART_CODE) Is Null)) 
ORDER BY DETAIL.BCI, DETAIL.FACTURE;

我不需要detail字段,因为它的详细信息。*在查询中(我会自己找到必要的)

这是多个JOIN,我不知道如何翻译它们。 我有每5个表的对象列表

即。

详细信息 - > LstDetail

A_ART - > LstA_ART

A_ENS - > LstA_ENS

A_LEA - > LstA_LEA

A_ART_1 - > LstA_ART(A_ART1是另一个连接的别名,而不是A_ART的别名)

A_ANAREPART - > LstA_ANAREPART

所以我开始写一些类似的东西:

MyList = (from itemDetail in LstDetail 
          join itemART in LstART 
            on itemDetail.DFA_ART equals itemART.ART_CODE 
          join itemANA_REPART in LstANA_REPART 
            on itemART.ART_ANA equals itemANA_REPART.ANA_CODE 
          select (...)).ToList();

但我不知道如何在查询中处理正确的连接。我应该进行多个linq查询吗?什么样的?

提前感谢您的答案

1 个答案:

答案 0 :(得分:0)

这样的事情可能会翻译,你可能需要调整案例,因为我使用了全部小写。此外,您需要选择如何处理detail.*,将d...替换为逗号分隔的详细信息列表d.field1, d.field2或类似detail = d以允许详细记录被列为对象。

var query = from d in detail
join a1 in a_art on d.dfa_art equals a1.art_code into art
from a in art.DefaultIfEmpty()
join ae1 in a_ens on d.dfa_art equals ae1.ens_code into ens
from ae in ens.DefaultIfEmpty()
join al1 in a_lea on ae.ens_code equals al1.lea_ens into lea
from al in lea.DefaultIfEmpty()
join a2 in a_art on al.art_code equals a2.lea_art into art1
from a1 in art1.DefaultIfEmpty()
join ar1 in a_ana_repart on a1.art_ana equals ar1.ana_code into ana
from ar in ana.DefaultIfEmpty()
where (ae.ens_code != null && a.art_code == null)
select new {
    d...,
    ae.ens_code,
    al.lea_art,
    a1.art_cha,
    a1.art_ana,
    ar.ana_cla2
};