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)
};
精彩评论