Compare Lists. How to?

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
 
Z

zacks

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;
}
}
}
 
J

Jon Skeet [C# MVP]

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
 
G

Göran Andersson

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);
}
 
J

Jon Skeet [C# MVP]

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?
 
T

Tim Jarvis

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.

--
 
J

Jon Skeet [C# MVP]

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.
 

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

Similar Threads

Linq Query 15
FindAll. Linq. 4
Intersection of Lists 5
Find Ints in List ... 4
List to CSV 3
Delete and Update 2
need help with a linq query 1
Compare two large dataTables 1

Top