简体   繁体   English

具有RIGHT JOIN的SQL查询的C#LINQ转换

[英]C# LINQ translation for a SQL query with RIGHT JOIN

I've got a query in ACCESS and i have to translate this query in C# 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;

I don't need exactly the detail fields as it's detail.* in the query (I'll found the necessary by myself) 在查询中,我并不需要确切的detail字段。*(我会自己找到必要的字段)

It's for the multiple JOIN that i don't know how to translate them. 我不知道如何翻译多个JOIN。 I've got object lists for each 5 tables 我有每5个表的对象列表

IE IE浏览器

Detail -> LstDetail 详细信息-> LstDetail

A_ART -> LstA_ART A_ART-> LstA_ART

A_ENS -> LstA_ENS A_ENS-> LstA_ENS

A_LEA -> LstA_LEA A_LEA-> LstA_LEA

A_ART_1 -> LstA_ART (A_ART1 is an alias for another join than with A_ART) A_ART_1-> LstA_ART(A_ART1是另一个联接的别名,而不是A_ART)

A_ANAREPART -> LstA_ANAREPART A_ANAREPART-> LstA_ANAREPART

So I begin to write something like : 所以我开始写类似:

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();

But I don't know how to handle the right join in the query. 但是我不知道如何处理查询中的正确联接。 Should I make multiple linq queries? 我应该进行多个linq查询吗? What kind? 哪一种?

Thanks in advance for answers 预先感谢您的回答

Something like this might translate, you may need to adjust case as I've used all lower case. 这样的事情可能会转化,您可能需要调整大小写,因为我已经使用了所有小写字母。 Additionally you need to choose how to deal with detail.* , either replace the d... with a comma separated list of fields from detail d.field1, d.field2 or something like detail = d to allow the detail record to be included as an object. 此外,您需要选择如何处理detail.* ,或者用逗号分隔的详细信息d.field1, d.field2或类似detail = d类的字段列表替换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
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM