By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

Delete from generic list using ForEach

P: n/a
I have a generic list object with a property called,
"MarkedForDeletion". During the course of my processing, some of the
objects in the list will get this property set to true and so at the
end I want to loop through my list and remove any object that has this
property set to true.

Currently I have code like this to accomplish this:

List<MyObjectdetails = MyMethodToGetDetails();
MyMethodToMarkDetailsForDeletion();

List<MyObjectfilteredDetails = details.GetRange(0, details.Count);
MyOjbect currentDetail = null;

for (int deleteIndex = 0; deleteIndex < details.Count; deleteIndex++)
{
currentDetail = details[deleteIndex];
if (currentDetail.MarkedForDeletion)
{
filteredDetails.Remove(currentDetail);
}
}

I want to look at replace this for loop with the ForEach method of the
generic list object. However, the problem is that the Action delegate
of this method does not allow me to pass anything other than the
specific object within the list that I'm dealing with. So I don't
know how to build a delegate that will allow me to remove the object
from within the overall list.

Does anyone have any ideas on how to do this?

Aug 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Aug 6, 2:20 pm, Doug <dnlwh...@dtgnet.comwrote:
I have a generic list object with a property called,
"MarkedForDeletion". During the course of my processing, some of the
objects in the list will get this property set to true and so at the
end I want to loop through my list and remove any object that has this
property set to true.
In that case, I suggest you use List<T>.RemoveAll with a predicate.

I didn't quite follow your example - if RemoveAll isn't enough for
your needs, could you write a short but complete example which
demonstrates the problem?

See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Aug 6 '07 #2

P: n/a
In that case, I suggest you use List<T>.RemoveAll with a predicate.

This was exactly what I needed and removes my need for the
"filteredDetails" object. Thank you!

Aug 6 '07 #3

P: n/a
A suggestion: your post was _very_ long for what appears to be a simple
question. You will get better answers if you can ask your questions
more concisely. I know this from personal experience, having some
problems being brief myself. :)
Hi Peter, sorry about that. In a previous post, Jon had suggested
posting my complete code and I tried to shrink it down as much as I
could because long posts can get confusing, but that was the best I
could come up with.

I think you have the gist of what I was looking for in that post. I
didn't put it into a seperate class, but I did have to create a member
level variable like you did in that class in ordre to do my work. I'm
not a big fan of member level variables (it's easy to lose track of
what they should be set to, are they set, etc, etc) but that's what I
went with.

Aug 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.