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