tags:

views:

50

answers:

3

Hello... I have two lists ...

List<ObjectA> listA
List<ObjectB> listB

Both have a int property ID, that is unique...

I´d like to get all objects from listA that have listA[x].ID = listB[x].ID, using LINQ...

Thanks

+2  A: 

You need to use the .Join(...) function.

In query syntax, it would look something like this:

var result = from a in listA
             join b in listB on a.ID equals b.ID
             select new { /* properties you want */ };

In standard C# syntax, it would look something like this:

var result = listA.Join(listB, a => a.ID, b => b.ID, (a, b) => new { /* properties you want */ });
NickLarsen
+2  A: 

This will do it:

from a in ListA
   join b in ListB on a.ID equals b.ID
   select new { ListAItem = a, ListBItem = b };

This will generate a list of anonymous objects each containing the ListA and ListB item where the IDs match.

Lazarus
+1  A: 
var query = listA.Where((x, i) => x.ID == listB[i].ID);
LukeH