Generic List .Find or .Contains

  • Thread starter Thread starter David
  • Start date Start date
D

David

I have two lists
List<MyClass> firstList = new List<MyClass>();
List<MyClass> secondList = new List<MyClass>();

The class MyClass has several properties (Id, Name, Title)

Both Lists (firstList, secondList) have several items in them.
I would like to know how to use either find,contains or exists
to see if the items in firstList are in secondList but the Id's won't
be the same.

I think I need to use a Predicate but can't seem to figure out
how to pass the values of the item in the firstList into the Predicate
for the secondList.

Basically I need to know if the Name and Title of an item in the
firstList are in the secondList.
 
David said:
I have two lists
List<MyClass> firstList = new List<MyClass>();
List<MyClass> secondList = new List<MyClass>();

The class MyClass has several properties (Id, Name, Title)

Both Lists (firstList, secondList) have several items in them.
I would like to know how to use either find,contains or exists
to see if the items in firstList are in secondList but the Id's won't
be the same.

I think I need to use a Predicate but can't seem to figure out
how to pass the values of the item in the firstList into the Predicate
for the secondList.

Basically I need to know if the Name and Title of an item in the
firstList are in the secondList.

Create a Dictionary<string, MyClass>, and add the items from one of the
lists, creating a key from the Name and Title of each item.

Then you can loop through the other list and look for the key for each
item in the dictionary.

It's much faster to look for an item in a dictionary than to search for
it in a list.
 
David said:
I have two lists
List<MyClass> firstList = new List<MyClass>();
List<MyClass> secondList = new List<MyClass>();

The class MyClass has several properties (Id, Name, Title)

Both Lists (firstList, secondList) have several items in them.
I would like to know how to use either find,contains or exists
to see if the items in firstList are in secondList but the Id's won't
be the same.

I think I need to use a Predicate but can't seem to figure out
how to pass the values of the item in the firstList into the Predicate
for the secondList.

Basically I need to know if the Name and Title of an item in the
firstList are in the secondList.
This is a way you can do it with an anonymous method.
You could do it with a traditional method too but this way will work.

List<string> l1 = new List<string>();
List<string> l2 = new List<string>();
foreach (string s in l1)
{
if (l2.Find(new Predicate<string>(delegate(string candidate)
{
return candidate == s;
})))
{
Console.WriteLine("found " + s);
}
else
{
Console.WriteLine(s + " not found");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top