Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generics - List.Exists Method

Posted on 2006-07-04
5
Medium Priority
?
1,600 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 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