Compare Lists. How to?

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have two lists, A and B, of a same class which has two properties:
ID and Name

A items have only the Name defined.

B items have the ID and the Name defined.

I want to create a new list, C (ID and Name), with the items in A
which Names exist in B.

Can I do this with Linq?

Will I need to use a for loop?

Thank You,
Miguel
 
Hello,

I have two lists, A and B, of a same class which has two properties:
ID and Name

A items have only the Name defined.

B items have the ID and the Name defined.

I want to create a new list, C (ID and Name), with the items in A
which Names exist in B.

Can I do this with Linq?

Will I need to use a for loop?

Thank You,
Miguel

Seem like the following code should do the trick, if I understand your
problem correctly.

Let's say the class that has the two properties, ID and Name, is named
Person.

C = new List<Person>();
foreach (Person personA in A)
{
foreach (Person personB in B)
{
if (personA.Name == personB.Name)
{
C.Add(personB);
break;
}
}
}
 
shapper said:
I have two lists, A and B, of a same class which has two properties:
ID and Name

A items have only the Name defined.

B items have the ID and the Name defined.

I want to create a new list, C (ID and Name), with the items in A
which Names exist in B.

Can I do this with Linq?

Will I need to use a for loop?

from a in A
join b in B where a.Name equals b.Name
select a
 
shapper said:
Hello,

I have two lists, A and B, of a same class which has two properties:
ID and Name

A items have only the Name defined.

B items have the ID and the Name defined.

I want to create a new list, C (ID and Name), with the items in A
which Names exist in B.

Can I do this with Linq?

Will I need to use a for loop?

Thank You,
Miguel

You could probably do that using LINQ, but not efficiently.

The most efficient way of doing that would be to put the items from the
B list in a dictionary, with the Name as key. Then you can loop through
the A list and check for the names in the dictionary.

Something like this:

Dictionary<string, TheClass> temp = new Dictionary<string, TheClass>();
foreach (TheClass item in B) temp.Add(item.Name, item);
List<TheClass> C = new List<TheClass>();
foreach (TheClass item in A {
TheClass found;
if (temp.TryGetValue(B.Name, out found)) C.Add(found);
}
 
Göran Andersson said:
You could probably do that using LINQ, but not efficiently.

The most efficient way of doing that would be to put the items from the
B list in a dictionary, with the Name as key. Then you can loop through
the A list and check for the names in the dictionary.

That's exactly what LINQ to Objects does. What made you think it would
be inefficient?
 
Jon said:
from a in A
join b in B where a.Name equals b.Name
select a

Hi Miquel,

There is also another option that might be worthwhile for you. It
sounds like the critera for testing the equality of your objects is
done mostly by the "Name" property, if this is the case then its
probably a good idea to create an IEqualityComparer<YourObj> class to
use in comparison operations. if / When you have one of these then you
can easily use the set operations of linq to do what you want.

i.e.

YourObj[] ListA = GetListAObjs();
YourObj[] ListB = GetListBObjs();

var IntersectedList = ListA.Intersect(ListB,new YourComparer());

Regards Tim.

--
 
Göran Andersson said:
Because my code runs four times faster.

Could you post a benchmark which shows this? LINQ certainly *should* be
doing it efficiently, basically implemented in the same way you
described. There's the slight inefficiency of calling a delegate, but
the implementations should have the same big-O efficiency.
 
Back
Top