2010-11-23 32 views
1

我試圖改變一些SQL到LINQ到SQL中,但是我在SQL以下行,我不知道如何轉換:ISNULL Linq中的總和語句內到SQL

SUM(Quantity * IsNull(ExchangeRate,1) * Factor) 

所以我至今寫的Linq的分組如下:

 var items = from item in _dataContext.GetTable<Trade>() 
        group item by new {item.Curve} 
        into grp 
        select new Model.Position 
           { 

            Curve = grp.Key.Curve, 
            Value = ... "That line here" 
           }; 
     return item 

我想過使用let關鍵字,並使用grp.Sum一直在努力,因爲在查詢的使用isnull嘗試。

任何幫助轉換此查詢將不勝感激!

理查德

回答

0

打字盲人(不含智能感知:d),但下面應該工作:

var items = from item in _dataContext.GetTable<Trade>() 
group item by new { item.Curve } into grp 
select new Model.Position 
{ 
    Curve = grp.Key.Curve, 
    Value = grp.Sum(i => i.Quantity * (i.ExchangeRate.HasValue ? i.ExchangeRate.Value : 1) * i.Factor) 
}; 
+0

這是完美的!沒想到可以在Sums裏面做條件感謝你的幫助! – Richard 2010-11-23 11:54:19