Solved

Generics - List.Exists Method

Posted on 2006-07-04
5
1,590 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
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…

737 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