2013-03-19 119 views
1

我有以下SQL查詢被轉換爲LINQ。我正在使用oracle數據庫。使用LINQ中where子句中的If語句

SELECT TableA.Id, 
     TableA.Date, 
     TableA.ItemId 
     TableB.Quantity, 
     TableB.Total 
FROM TableA, TableB, TableC, TableD 
Where TableA.Id = TableB.Id and 
     TableA.Id = TableC.Id (+) and 
     TableA.Id = TableD.Id (+) and 
     TableA.ItemId = _itemId and 
     TableA.Date >= _from_date and 
     TableA.Date < _to_date and 
     DECODE(TableD.Id,NULL,0,1) = (some boolean variable) 

表A和表C具有一個< - >(一個或零),其中表C是可選的。

LINQ查詢我寫的是:

var data = from ta in context.TableAs 
       join tB in context.TableBs 
        on tA.Id equals tB.Id 
       join tD in context.TableDs 
        on tA.Id equals tD.Id into A 
      from itemA in A.DefaultIfEmpty() 
       join tC in context.TableCs 
        on itemA.tA.Id equals tC.Id into B 
      from itemB in B.DefaultIfEmpty() 
      where itemA.tA.ItemId == _itemId && 
       itemA.tA.Date >= _startDate && 
       itemA.Ta.Date< _endDate && // this is where I got stuck... 

      select new 
      {        
       itemA.tA.Id, 
       itemA.tA.Date, 
       itemA.tA.ItemId, 
       itemA.tB.Quantity, 
       itemA.tB.BalanceQuantity,        
      }; 

我怎麼能寫我的構建LINQ查詢的WHERE子句中的SQL QUERTY最後一行(即

DECODE(TableD.Id,NULL,0,1) = some boolean variable) 

非常感謝...

+0

你可以這樣寫:tD.Id ??真正。我假設你的數據庫中的ID是位型的 – 2013-03-19 06:31:45

回答

2

Oracle的decode基本上是一個switch stat EMENT:

decode(value, case1, result1, case2, result2, ..., defaultresult) 

與經典的例子:

select decode(supplier_id, 10000, 'IBM', 
          10001, 'Microsoft', 
          10002, 'Hewlett Packard', 
          'Gateway') as result 
from suppliers; 

因此,對於你的查詢:

decode(TableD.Id,null,0,1) = some boolean variable) 

一個LINQ等效可能是:

(itemA.tD.Id != DBNull.Value) == some boolean variable 
+0

非常感謝你......它工作的很棒!!! 1 – Shpongle 2013-03-19 07:07:05