Solved

Generics - List.Exists Method

Posted on 2006-07-04
5
1,576 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now