Solved

Generics - List.Exists Method

Posted on 2006-07-04
5
1,585 Views
Last Modified: 2008-03-17
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!
0
Comment
Question by:marmaxx
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 17040293
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
0
 

Author Comment

by:marmaxx
ID: 17040360
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.
0
 
LVL 44

Accepted Solution

by:
bruintje earned 500 total points
ID: 17040392
that was added later in the same blog entry

2 ways actually, the first one using the predicate delegate

and another one

--
foreach( string s in returnedItemList )
{

    bool found = returnedItemList.Exists( delegate(string t) { return item == t; } );

    if( !found) { newList.Add( item ); }
}
--

hope this helps a bit more
0
 

Author Comment

by:marmaxx
ID: 17043625
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.
0
 

Author Comment

by:marmaxx
ID: 17146642
I was able to get this to work with a little 'tweak' here and there. Thanks for the help!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question