C
C#3y ago
Lounder

Help with Linq

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