2014-06-14 103 views
1

JOIN我有一個SQL查詢:顯示空結果使用LINQ

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 

寫在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") 
        };  

我有沒有記錄一個GridView數據顯示,SQL查詢返回我需要的內容,但Linq查詢返回完全相反。

回答

0

我不會用一個outer join這一點,但解析爲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") 
      };