开发者

How can i use "COALESCE(SUM..." in linq?

开发者 https://www.devze.com 2023-01-05 04:54 出处:网络
i try to use sum and Coalesce . How can i translate to linq? SELECT @SumQtyOut=COALESCE(SUM(Qty),0) FROM dbo.StockMovement WHERE FromLocationType=@FromLocationType AND

i try to use sum and Coalesce . How can i translate to linq?

 SELECT @SumQtyOut=COALESCE(SUM(Qty),0) FROM dbo.StockMovement WHERE FromLocationType=@FromLocationType AND
         * FromNo=@FromNo AND FromSeq=@FromSeq AND ItemTypeNo=@ItemTypeNo AND ItemID=@ItemID

i do sometihng :

 using (StockProcedureDataContext stock = new StockProcedureDataContext())
                {
                    SumQtyOut = from s in stock.StockMovements 
                                where s.FromLocationType == FromLocationType &&
                                s.FromNo== FromNo && 
                                s.FromSeq == FromSe开发者_运维技巧q &&
                                s.ItemTypeNo == ItemTypeNo &&
                                s.ItemID == ItemID select 
                }


This snippet should yield the result you are looking for.

using (StockProcedureDataContext stock = new StockProcedureDataContext())
{
    var items = from s in stock.StockMovements 
                where s.FromLocationType == FromLocationType &&
                s.FromNo== FromNo && 
                s.FromSeq == FromSeq &&
                s.ItemTypeNo == ItemTypeNo &&
                s.ItemID == ItemID 
                select s.Qty ?? 0;
    SumQtyOut = items.Sum(x => x);
}

select s.Qty ?? 0 returns 0 if s.Qty is null. items.Sum(x => x) summs up the quantities you have selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消