简体   繁体   中英

Show null results through the LEFT JOIN with Linq

I have a SQL query:

SELECT A.Codplano, A.Secao, A.Setor,A.Subsetor,A.Contato, ISNULL(B.Subord,'NÃO
LANÇADA')AS Situacao 
FROM vwPLANODIN A LEFT JOIN LANCADA B
ON A.Codplano = B.Subord
and B.Data = '2014-06-10'
WHERE B.ID IS NULL and A.Sitio = 7341

Written in Linq:

    var cob = from A in dataClass.vwPLANODINs
                  join B in dataClass.LANCADAs on new { A.Codplano, Data = data }
                  equals new { Codplano = B.Subord, Data = Convert.ToString(B.Data) }  into B_join
                  from B in B_join.DefaultIfEmpty()
                  where
                       B.Data == null &&
                       A.Sitio == local
                  select new
                  {
                      A.Codplano,
                      A.Secao,
                      A.Setor,
                      A.Subsetor,
                      A.Contato,
                      Situacao = (B.Subord ?? "N/A")
                  };    

I have to show in a Gridview data not recorded, the SQL query returns what I need, but the Linq query, returns the exact opposite.

I would not use an outer join for this, but a query that resolves to a not exists :

var cob = from A in dataClass.vwPLANODINs
          where !(from B in dataClass.LANCADAs 
                  where B.Subord == A.Codplano && B.Data == data
                  select B)
                .Any()
             && A.Sitio == local
          select new
          {
              A.Codplano,
              A.Secao,
              A.Setor,
              A.Subsetor,
              A.Contato,
              Situacao = (B.Subord ?? "N/A")
          };  

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