Subtract columns in a table using LINQ

I need to subtract one column from another column in a table in my database using LINQ

Trial table

Userid Money Type x 500 + y 250 + x 300 - y 100 - x 120 - 

I need to do this

500 - 300 - 120 at x

and

250 - 100 for y.

how to do it?

I tried grouping like trial.userid and trial.money , trial.type . Basically, I think that I need to group users by id, and I need to add + and subtract - from the sum of the addition, please help me.

+4
source share
2 answers

How about something like:

 var query = from row in db.Rows group row by row.UserId into tmp select new { UserId = tmp.Key, Money = tmp.Sum(x => x.Type == '+' ? x.Money : -x.Money) }; 
+6
source

Try something similar for each group:

 group.Sum(row => row.Type == "+" ? row.Money : -row.Money) 
+4
source

Source: https://habr.com/ru/post/1343134/


All Articles