I am trying to find common rows in 2 lists as follows:
public class ListA
{
public string Id { get; set; }
public string Name { get; set; }
}
List<ListA> listA = new List<ListA>();
List<string> listB = new List<string>();
List<ListA> intersect = listA.Where(x => listB.Any(y => y == x.Id)).ToList();
So the above Linq query will get me all common items in the 2 lists based on Id property, How can I do the same using Intersect/IntersectBy Id and return the type of ListA?
Use the IntersectBy
LINQ method. This will let you compare the Id property from each ListA entry against another list of the same type as the extracted property.
IEnumerable<ListA> intersect = listA.IntersectBy(listB, a => a.Id);