Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove from List using Linq

So, I have a List of objects of class A that contains a List of objects of class B

class A 
{
  ...
  List<B> bs;
}

and I have lists:

List<A> mainList;
List<B> listForRemoval;

How can I, using Linq, "clean" mainList, by removing all objects from bs (for every A in mainList) that exists in listForRemoval?

I hope I didn't confuse you with this question. :)

like image 218
Draško Avatar asked May 19 '26 15:05

Draško


1 Answers

linq itself is probably not a great fit, but you can use some of it's extension methods. Linq typically is mostly for selection, not processing.

mainList.ForEach(x=>x.bs = x.bs.Where(y=>!listForRemoval.Contains(y)).ToList());
like image 145
ExCodeCowboy Avatar answered May 22 '26 05:05

ExCodeCowboy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!