How can I replicate following in LINQ
SELECT KEY_COLUMN,
CASE WHEN SUM(COLUMN2) < 0 THEN SUM(COLUMN2) ELSE 0 END AS NewColumn
FROM TABLE
GROUP BY KEY_COLUMN
till now I could come up with following
var result = from a in source
group a by a.Key_Column
into g
select new
{
Key_Column = g.Key,
NewColumn = g.Where(item => item.Column2 < 0)
.Sum(item => item.Column2)
};
above code is not really summing up the Column2 before deciding if it’s a negative number or not.
1 Answer