By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

Compare Lists. How to?

P: n/a
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
Jul 1 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Jul 1, 4:09*pm, shapper <mdmo...@gmail.comwrote:
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;
}
}
}
Jul 1 '08 #2

P: n/a
shapper <md*****@gmail.comwrote:
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

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 1 '08 #3

P: n/a
shapper wrote:
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, TheClasstemp = new Dictionary<string, TheClass>();
foreach (TheClass item in B) temp.Add(item.Name, item);
List<TheClassC = new List<TheClass>();
foreach (TheClass item in A {
TheClass found;
if (temp.TryGetValue(B.Name, out found)) C.Add(found);
}

--
Göran Andersson
_____
http://www.guffa.com
Jul 1 '08 #4

P: n/a
Göran Andersson <gu***@guffa.comwrote:
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 Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 1 '08 #5

P: n/a
Jon Skeet [C# MVP] wrote:
shapper <md*****@gmail.comwrote:
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
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<YourObjclass 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.

--

Jul 1 '08 #6

P: n/a
Jon Skeet [C# MVP] wrote:
Göran Andersson <gu***@guffa.comwrote:
>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?
Because my code runs four times faster.

--
Göran Andersson
_____
http://www.guffa.com
Jul 3 '08 #7

P: n/a
Göran Andersson <gu***@guffa.comwrote:
Jon Skeet [C# MVP] wrote:
Göran Andersson <gu***@guffa.comwrote:
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?
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 3 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.