18 Replies
I have to remove
num
-s from universe
in each iterationLinq is made to query items, not edit a collection
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
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
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.
Or see if you can do this as a "filtering" step which is a lot more intuitive if thinking along LINQ lines.
I've never heard of filtering before. Can you elaborate?
You could filter out the items to remove and overwrite the list with the new items
"filter" in the case of LINQ is .Where, essentially.
How should my .Where look? I didn't think it was possible in this situation
Well, that's tricky because I don't know why you're doing this
It's really hard and long to explain so it's best to not get into the exercise description
OK, well, maybe just be open to the idea that you should step back a step or two conceptually then
Basic idea of where is you pass it a function that defines if an item is to be kept
I'm thinking whether I should scrap this whole approach and go again in a different direction
yeah
and add ToList at the end to convert the query into a collection
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
Alright, thanks everyone
<3