473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Compare Lists. How to?

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
7 9843
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Fazer | last post by:
Hello, I was wondering which would be the best way to compare a list? I was thinking of just using a for loop and testing the condition. What do you guys think? The best/fastest way of...
6
by: Robin Siebler | last post by:
I have two directory trees that I want to compare and I'm trying to figure out what the best way of doing this would be. I am using walk to get a list of all of the files in each directory. I...
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
3
by: Frank Esser | last post by:
Hi, if there are two objects of type "SortedList", how can I compare them? I want to find out whether they have exactly the same values and keys in the same sort order but I do not want to loop...
4
by: Justin Emlay | last post by:
I have two lists. These can be in either table form or array. That is, my data is in a dataset which I can move to an array if need be. ListA is a master list and it contains all items. ListB...
1
by: Ron | last post by:
Hi, I need to prefome a sync process on two Lists Lets say i have two Lists of type string. And each list can be dimensioned differently, but will eventually contain the same amount of elements...
4
cassbiz
by: cassbiz | last post by:
What I would like to do is run two queries with a single output. for example my first query is: <? $query = "(SELECT spacenummer FROM res WHERE arrival BETWEEN '$var_from' AND '$var_to' OR...
6
by: jairodsl | last post by:
Hello everybody ! I have two list, they are, S1=, and S2=, but i have to compare both in this way: A vs J A vs I, B vs J A vs H, B vs I, C vs J A vs G, B vs H, C vs I, D vs J
0
gits
by: gits | last post by:
This little article will show you how to optimize runtime performance when you need to compare two arrays (a quite common task). Have a close look at the entire article, and you will see the...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.