473,241 Members | 1,471 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,241 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 9837
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.