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

Using ForEach and FindAll of Generics

P: n/a
Hi,

I have a collection of objects and I need to compare each item in the
collection to see if it's a match on any or all of the others. I have
stored my collection in an item like so: "List<PossibleDupsdups"
where PossibleDups is an object of my own creation.

I have been looking at how the ForEach and FindAll methods work and
here is what I was thinking might be a good approach to doing this (in
pseudocode):

1. Use the ForEach method to loop through each item in the
collection.
2. Use the FindAll method inside my Action delegate of the ForEach
method to do my comparison.

This would be pretty neat to get it to work like this (very little
code compared to what I had planned). But I think it won't work as I
would have to do something like this:

private void FindActualDuplicates(List<PossibleDupsdups)
{
possibleDuplicates.ForEach(CompareItems);
}

private void CompareItems(PossibleDups dup)
{
bool isDup = possibleDuplicates.FindAll(FindDups);
}

private static bool FindDups(PossibleDups dup)
{
//Code to determine if dups.
}

If you look at my CompareItems method you can see my problem. I don't
have a clear way to get the Array List into that method (even tho I
coded in the method as if I did get it in there) because the ForEach
only sends one item of that list in at a time.

I know that I could bypass the ForEach altogether and do a FindAll
over the entire collection, but I need to do something like this:

1. Compare object 1 in the collection to object 2, 3, 4.
2. Compare object 2 in the collection to object 3, 4.
3. Compare object 3 in the collection to 4.

In essence, if object 1 is a match to just 2. I still need to see if
3 is a match to just 4 (and so on depending on the number of objects
in the collection). Because of this I don't know if I can get away
with not using the ForEach (or using the old foreach method).

Does anyone have any suggestions? Is this possible? Am I
overthinking it?

Jul 19 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Thank you Nick and Peter, I will look into both your suggestions.
Peter, I think I understood yours (but some of it seemed a bit over my
head :) ) so we'll see how it goes. I'll let you know how it turns
out.

Jul 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.