Delete from generic list using ForEach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug

    #1

    Delete from generic list using ForEach

    I have a generic list object with a property called,
    "MarkedForDelet ion". 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<MyObjectde tails = MyMethodToGetDe tails();
    MyMethodToMarkD etailsForDeleti on();

    List<MyObjectfi lteredDetails = details.GetRang e(0, details.Count);
    MyOjbect currentDetail = null;

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

    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?

  • Jon Skeet [C# MVP]

    #2
    Re: Delete from generic list using ForEach

    On Aug 6, 2:20 pm, Doug <dnlwh...@dtgne t.comwrote:
    I have a generic list object with a property called,
    "MarkedForDelet ion". 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>.RemoveA ll 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

    Comment

    • Doug

      #3
      Re: Delete from generic list using ForEach

      In that case, I suggest you use List<T>.RemoveA ll with a predicate.

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

      Comment

      • Doug

        #4
        Re: Delete from generic list using ForEach

        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.

        Comment

        Working...