Linq query list contains a list

Are you trying to get a list of ObjectBs where all of the ObjectAs are in ListOfIdsA, or any of them?

I think you want either:

ListObjectB.Where(p => p.ListOfObjectA.Any(x => ListOfIdsA.Contains(x.Id)))

or

ListObjectB.Where(p => p.ListOfObjectA.All(x => ListOfIdsA.Contains(x.Id)))

(You may well want to make ListOfIdsA a HashSet<string> if it’s of significant size, btw.)

Leave a Comment