|
|
|
start date: Mon, 06 Aug 2007 06:21:45 -0700,
posted on: microsoft.public.dotnet.framework
back
| Thread Index |
|
1
Doug
|
|
2
Peter Duniho
|
|
3
Frans Bouma [C# MVP]
|
Delete from generic list using ForEach
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<MyObject> details = MyMethodToGetDetails();
MyMethodToMarkDetailsForDeletion();
List<MyObject> filteredDetails = 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?
Date:Mon, 06 Aug 2007 06:21:45 -0700
Author:
|
Re: Delete from generic list using ForEach
Doug wrote:
> [multi-posted message]
Please do not multi-post messages. This question was already answered
in the m.p.dotnet.languages.csharp newsgroup, but of course there's no
indication of that here because you multi-posted.
Learn to cross-post correctly, including all your intended newsgroups in
a single post, if you must cross-post at all.
Thank you,
Pete
Date:Mon, 06 Aug 2007 10:27:12 -0700
Author:
|
Re: Delete from generic list using ForEach
Doug wrote:
> 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<MyObject> details = MyMethodToGetDetails();
> MyMethodToMarkDetailsForDeletion();
>
> List<MyObject> filteredDetails = 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?
details.RemoveAll(delegate(MyObject m) { return m.MarkedForDeletion;
});
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Date:Tue, 07 Aug 2007 01:03:40 -0700
Author:
|
|
|