472,145 Members | 1,432 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

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.

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.

Jul 5 '06 #1
2 6990
Try the List<T>.Find method.
http://msdn2.microsoft.com/en-us/library/x0b5b5bc.aspx You can use a method
shown in the sample code or an anonymous delegate.

Predicate<Tmatch = delegate(T item)
{
return item.Id == REQUIRED_ID;
};

T found = list.Find(match);

if (found != null)
{
//move/copy found to list2
}

"sianan" <sm*********@tds.netwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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.

Jul 5 '06 #2
"sianan" <sm*********@tds.netwrote:
(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<YourTypelistToDoStuffWith = 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.
The Contains method of List<Tcannot be overridden. It can only be
hidden with a new definition.
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

--
http://barrkel.blogspot.com/
Jul 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tim Richardson | last post: by
4 posts views Thread by DEWright_CA | last post: by
1 post views Thread by BobAchgill | last post: by
2 posts views Thread by Tarscher | last post: by
5 posts views Thread by John Cantley | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.