简体   繁体   中英

How to perform a check on a field condition in a LINQ query?

I have a LINQ query that performs some joins on other tables, but I need to perform a check on a Join condition in order to know if a field is null, because if it is null I have to join with the same table but on another field.

I have this LINQ query :

var fasi = _fasi.Split(';').Select(n => short.Parse(n)).ToList();
listaDoc = (
    from Documenti in db.Documenti
    join Tipi in db.Tipi
        on new { C = (int)Documenti.Classe, C_Tipo = Documenti.CTipo }
        equals new { Tipi.C, Tipi.C_Tipo } into Tipi_join
    from Tipi in Tipi_join.DefaultIfEmpty()
    join Stipi in db.Stipi
        on new { C = Documenti.Classe, C_STipo = Documenti.CSTIpo }
        equals new { Stipi.C, Stipi.C_STipo } into Stipi_join
    from Stipi in Stipi_join.DefaultIfEmpty()
    join PathClassi_Web in db.PathClassi_Web on new { cod_class = Documenti.Classe } equals new { cod_class = PathClassi_Web.cod_class } into PathClassi_Web_join
    from PathClassi_Web in PathClassi_Web_join.DefaultIfEmpty()
    where
        (fasi).Contains((short)Documenti.Stato)
    select new
    {
        Documenti.ID,
        Documenti.Data,
        Documenti.Descrizione,
        Documenti.DAL,
        Documenti.AL,
        Documenti.Stato,
        Documenti.NomeDoc,
        Classe = (int?)Documenti.Classe,
        Documenti.CTipo,
        Documenti.CSTIpo,
        Documenti.SiglaRed,
        Documenti.Stipula,
        Documenti.DataCmp,
        Documenti.Regione,
        Documenti.NumDoc,
        Documenti.Destinazione,
        Documenti.DataStampa,
        Documenti.Ext,
        GRP2 = Tipi.GRP2,
        DESCRIZ = Stipi.Descriz,
        DESCR_CLASS = PathClassi_Web.descr_class
});

I need to know if Tipi.C_Tabella is null. If not, I will join Documenti and Tipi "on Tipi.C_Tabella = Documenti.CTipo , but if it is null I will join Documenti and Tipi on Tipi.C_Tipo = Documenti.CTipo .

How can I do this by modifying my LINQ query written above?

from Documenti in db.Documenti
join Tipi in db.Tipi
on new { C = (int)Documenti.Classe, C_Tipo = Documenti.CTipo }
equals new { Tipi.C, Tipi.C_Tabella ?? Tipi.C_Tipo } into Tipi_join

OR

from Documenti in db.Documenti
from Tipi in db.Tipi
where (Tipi.C_Tabella ?? Tipi.C_Tipo) = Documenti.CTipo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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