Link to home
Start Free TrialLog in
Avatar of marmaxx
marmaxx

asked on

Generics - List.Exists Method

Hello,

I am very new to generics and looking for an example showing how to use the List.Exists delegate. I want to search a particular list to see if an item exists in it.  If the item doesn't exist, I want to add it to another list (I'm trying to test for duplicates).

I'm trying to do something like this (I know the syntax is incorrect.):

foreach (myListItem item in myItemList
{
     if (!returnedItemList.Exists(item) // not sure of syntax here
    {
         newList.Add(item);
    }
}


How would I perform this check using the Predicate delegate?

Thanks!
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Hi marmaxx,
----------

funny a likewise question was blogged here
http://channel9.msdn.com/ShowPost.aspx?PostID=76961

not sure about it but in that instance the Contains function was needed is that what you need?

foreach (myListItem item in myItemList
{
     if (!returnedItemList.Contains(item)
    {
         newList.Add(item);
    }
}

----------
bruintje
share what you know, learn what you don't
Avatar of marmaxx
marmaxx

ASKER

Hi bruintje,

No, unfortunately I can't use the Contains method. The program I am working with uses a different implementation of contains (passing in an object reference rather than a value). Of course, I might not be explaining this very well, since I'm only just starting with generics. Either way, I've go to figure out how to do this using Exists, with the Predicate.
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marmaxx

ASKER

Nope, couldn't get the example to work.

What I am trying to do is compare two Lists: List1 and List2. I want to loop through each item of List1 to determine if it exists in List2. If it does, I want to add it to another list. Each of the list items has an ID property, which I need to convert to string, and compare.

I wasn't able to figure out how to do this using the example.
Avatar of marmaxx

ASKER

I was able to get this to work with a little 'tweak' here and there. Thanks for the help!