subtraction qty from a list by linq

elizondo82

Member
Joined
Sep 16, 2022
Messages
12
Programming Experience
10+
Hello, I search from Google a lot example, but some codes is not working.
I try display a list of sku,qty,total,etc...

Example:

ID | Sku | Total | Qty
1 87 58.0 10
2 87 58.0 5
3 87 58.0 5
4 125 15.2 45
5 125 15.2 45

I want subtraction the qty of sku = 87, the subtraction is 18

so the result is:

ID | Sku | Total | Qty
1 87 58.0 0
2 87 58.0 0
3 87 58.0 2
4 125 15.2 45
5 125 15.2 45

I see from SQL server is with Over(order by id) there is working, but I want with C# Linq, some idea?
Thanks!!
 
LINQ is a way to GET data. If you want to subtract a value from another value in that data then you have to get the data first, then do the subtraction. They are two separate operations. If you have found examples of LINQ queries then you know how to do the query and I would hope that you wouldn't need to be shown how to do a subtraction.
some codes is not working.
What code? How is it not working? You need to show us what you've done and tell us what happened so we can see how you're thinking and what's wrong with it. The idea is that we help you fix the specific problem you're having, not that you tell us what you want to achieve and we write your code for you.
 
What that SO question shows is quite different to what you're asking for. I've already told you what you need to do: get the data first, then do the subtraction. Instead of trying to pluck code out of the air without knowing what it actually has to do, spend some time to consider the logic first, then write code to explicitly implement that logic. How would you do this if it was a manual process? You would get all the data first, then you would go through the rows one by one, looking for those with the appropriate SKU. When you found one, you would look at the quantity. You'd then do the appropriate arithmetic and, if there was still more to subtract, continue on to the next row. That's what you need to do in code too.
 
It's not easy to use LINQ for this; use a normal loop instead. Just because LINQ is a hammer doesn't mean every problem a nail
 
Back
Top Bottom