繁体   English   中英

在LINQ的where子句中使用If语句

[英]using If statement inside where clause in LINQ

我将以下SQL查询转换为LINQ。 我正在使用oracle DB。

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)

TableA和TableC具有一个<->(一个或零),其中TableC是可选的。

我写的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,                               
           };

如何在SQL查询中写最后一行(即

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

在我构造的LINQ查询的where子句中?

非常感谢...

Oracle的decode基本上是一个switch语句:

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

暂无
暂无

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

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