开发者

IsNull inside Sum statement in Linq to Sql

开发者 https://www.devze.com 2023-01-26 17:54 出处:网络
I\'m trying to change some SQL into Linq-to-Sql, however I have the following line in SQL that I\'m not sure how to convert:

I'm trying to change some SQL into Linq-to-Sql, however I have the following line in SQL that I'm not sure how to convert:

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

So I've so far written the grouping Linq as follows:

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

                                   Curve = grp.Key.Curve,
            开发者_JAVA百科                       Value = ... "That line here"
                               };
        return item

I've thought of using the let keyword, and tried using grp.Sum have struggled as there's the IsNull in the query.

Any help converting this query would be greatly appreciated!

Richard


Typing blind (without intellisense :D) but the following should work:

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

精彩评论

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