Reputation: 23
I have searched all the forum, but I could not find an answer for my question. Maybe I just used the wrong keywords. Let's assume I have a class Foo:
class Foo
{
string a;
string b;
}
A number of instances of Foo are now saved in a
BindingList<Foo> bf;
Then I have a second class which realizes some sort of relation between objects of type Foo:
class Bar
{
Foo f1;
Relation r;
Foo f2;
}
Again, a number of instances of class Bar are saved in a
BindingList<Bar> bb;
At a certain point in my program, I now delete an instance of Foo, say f7.
bb.Remove(f7);
Is there any possibility, that exactly those instances of Bar are deleted that hold a reference to f7? At the moment, I hook on the ListChanged event of bf and manually traverse bb and kill those instances of Bar that contain f7. Is there a smarter way?
Thanks for your help, Guido
Upvotes: 1
Views: 357
Reputation: 6444
Judging by your OP. I suspect that you want something like this. You would have to ensure that your Foo
objects in Bar
are the same instance, or implement IEquatable
on Foo
.
var barWhereFoosExist = bb.Where(x => x.f1.Equals(f7) || x.f2.Equals(f7));
foreach(Bar b in barWhereFoosExist)
{
bb.Remove(b);
}
Try the above using something like this.
BindingList<Bar> bb = new BindingList<Bar>();
Foo f7 = new Foo();
Bar b = New Bar();
b.f1 = f7;
b.f2 = new Foo();
Upvotes: 1