Using List.Exists Method With Predicate

S

sianan

I am having a problem doing the following in generics.

I have two list of a custom item type. I need to iterate through the
first list and match each item against another list to see if there is
a match. If a match is found, I need to move the matched item into
another list.

I want to compare the objects by value. The items in each list have an
ID property (of type object) which I want to convert to a strng in
order to be able to do this.

I cannot use the Contains method to perform this check. Contains has
been overridden. I also run the risk of breaking the control (which
someone else wrote) if I modify this behavior.

I hope the above explanation makes sense.

I know that I can create a method which will be used as a predicate to
check for the specified condition (a match) but I have been unable to
get the syntax right.

Does anyone have some idea of how I might go about this? Any help
(particularly example code) would be much appreciated.
 
B

Barry Kelly

sianan said:
(Title: Using List.Exists Method With Predicate)

I am having a problem doing the following in generics.

I have two list of a custom item type. I need to iterate through the
first list and match each item against another list to see if there is
a match. If a match is found, I need to move the matched item into
another list.

I want to compare the objects by value. The items in each list have an
ID property (of type object) which I want to convert to a strng in
order to be able to do this.

So, you mean something like:

---8<---
// Word of warning: this takes time proportional to n*m
List<YourType> listToDoStuffWith = list.FindAll(delegate(YourType value)
{
return otherList.Exists(delegate(YourType otherValue)
{
// Maybe use some other string.Equals() overload here
return value.ID.ToString() == otherValue.ID.ToString();
});
});

list.RemoveAll(listToDoStuffWith.Contains);
// or alternatively:
listToDoStuffWith.ForEach(list.Remove);

listToAddTo.AddRange(listToDoStuffWith);
--->8---
I cannot use the Contains method to perform this check. Contains has
been overridden.

I know that I can create a method which will be used as a predicate to
check for the specified condition (a match) but I have been unable to
get the syntax right.

You can create a static or instance method with the same signature as
the anonymous delegates I used above (i.e. taking one argument of your
list type and returning a boolean value). I personally think anonymous
delegates are easier to read, because otherwise you end up with a method
that is textually distant from where it is used, yet intrinsically
linked with the implementation of the body of the calling method.

It makes sense to move from an anonymous delegate to a separate method
if it becomes possible to reuse the predicate.

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top