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
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
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 */ });
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.