C
C#2y ago
Lounder

Help with Linq

The image should be enough to explain what I want to achieve
18 Replies
Lounder
Lounder2y ago
I have to remove num-s from universe in each iteration
TheBoxyBear
TheBoxyBear2y ago
Linq is made to query items, not edit a collection
Lounder
Lounder2y ago
I want to exclude nums that are contained in universe because they mess up my ordering I agree but I dont know a better way to do it I even doubt whether it's possible with linq
amio
amio2y ago
Yeah, I wanna say maybe this should not be one "step" in LINQ at the very least, you're mixing in a ton of side effects and that's usually a no-no
TheBoxyBear
TheBoxyBear2y ago
Use a loop. Store either the items or the indices to remove and remove them after the loop Editing a list during foreach enumeration throws an exception and using for can get messy with the index as items are added or removed.
amio
amio2y ago
Or see if you can do this as a "filtering" step which is a lot more intuitive if thinking along LINQ lines.
Lounder
Lounder2y ago
I've never heard of filtering before. Can you elaborate?
TheBoxyBear
TheBoxyBear2y ago
You could filter out the items to remove and overwrite the list with the new items
amio
amio2y ago
"filter" in the case of LINQ is .Where, essentially.
Lounder
Lounder2y ago
How should my .Where look? I didn't think it was possible in this situation
amio
amio2y ago
Well, that's tricky because I don't know why you're doing this
Lounder
Lounder2y ago
It's really hard and long to explain so it's best to not get into the exercise description
amio
amio2y ago
OK, well, maybe just be open to the idea that you should step back a step or two conceptually then
TheBoxyBear
TheBoxyBear2y ago
Basic idea of where is you pass it a function that defines if an item is to be kept
Lounder
Lounder2y ago
I'm thinking whether I should scrap this whole approach and go again in a different direction yeah
TheBoxyBear
TheBoxyBear2y ago
and add ToList at the end to convert the query into a collection
amio
amio2y ago
Because iterating over a list and removing stuff is generally easy to rephrase as "this list with/without X" and if it isn't, that's... kinda odd
Lounder
Lounder2y ago
Alright, thanks everyone <3