PC Review


Reply
Thread Tools Rate Thread

Compare Lists. How to?

 
 
shapper
Guest
Posts: n/a
 
      1st Jul 2008
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
 
Reply With Quote
 
 
 
 
zacks@construction-imaging.com
Guest
Posts: n/a
 
      1st Jul 2008
On Jul 1, 4:09*pm, shapper <mdmo...@gmail.com> 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


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;
}
}
}
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Jul 2008
shapper <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
Göran Andersson
Guest
Posts: n/a
 
      1st Jul 2008
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, 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
_____
http://www.guffa.com
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Jul 2008
Göran Andersson <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
Tim Jarvis
Guest
Posts: n/a
 
      2nd Jul 2008
Jon Skeet [C# MVP] wrote:

> shapper <(E-Mail Removed)> wrote:
> > 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<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.

--

 
Reply With Quote
 
Göran Andersson
Guest
Posts: n/a
 
      3rd Jul 2008
Jon Skeet [C# MVP] wrote:
> Göran Andersson <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      3rd Jul 2008
Göran Andersson <(E-Mail Removed)> wrote:
> Jon Skeet [C# MVP] wrote:
> > Göran Andersson <(E-Mail Removed)> wrote:
> >> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare two lists lark Microsoft Excel Worksheet Functions 3 6th May 2008 02:12 AM
compare two lists rob p Microsoft Excel Misc 3 10th Oct 2005 07:01 PM
compare two lists rob p Microsoft Excel Worksheet Functions 3 10th Oct 2005 07:01 PM
Re: Compare two lists Don Guillett Microsoft Excel Programming 2 30th Apr 2004 12:08 AM
Compare Lists David Willey Microsoft Excel Programming 3 8th Aug 2003 03:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:37 PM.