Check if list contains item from other list in EntityFramework Check if list contains item from other list in EntityFramework database database

Check if list contains item from other list in EntityFramework


I'll suggest:

var searchIds = new List<int>{1,2,3,4,5};var result = persons.Where(p => p.Locations.Any(l => searchIds.Contains(l.Id)));

Contains will be translated to IN statement.

Keep in mind that the id list goes into the sql statement. If your id list is huge then you'll end up having a huge query.


Try switching to joins instead of doing a massive data include:

var searchIds = new List<int>{1,2,3,4,5};var results = (from p in persons               join l in Location on p.PersonId equals l.PersonId               where searchIds.Contains(l.Id)               select p).Distinct().ToList();

Obviously fix this line to match your classes and/or join property.

join l in Location on p.PersonId equals l.PersonId

I would expect that to generate a more friendly execution plan.