suppose I have the following table:
user type amount
2 a 10
2 b 20
2 c 40
3 a 15
3 b 20
3 c 45
I want to replace (c) amount with (c - (a+b)) grouping by the user and type, how could I do this? thanks
Items select new { Sum(p. Total), Sum(p. Done)};
In LINQ, aggregation functions are those functions which are used to calculate a single value from the collection of the values.
Count() methodIEnumerable<string> strings = new List<string> { "first", "then", "and then", "finally" }; // Will return 4 int result = strings. Count(); NOTE: The Count() LINQ method (an extension method to IEnumerable<T> ) is slightly different from the Count property on List<T> .
Here is a way:
UPDATE (now using Sum
):
from item in table
group item by item.user into g
let a = (from i in g where i.type == "a" select i.amount).Sum()
let b = (from i in g where i.type == "b" select i.amount).Sum()
let c = (from i in g where i.type == "c" select i.amount).Sum()
select c - (a + b);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With