entity-frameworkjoiniqueryablerelated-content

How to get entities that a collection property has any of the elments of a list?


I have this entities:

MyEntityA
{
    long IDEntityA;
    List<EntityB> lstEntityB;
    .... (other properties);
}

MyEntityB
{
    long IDEntityB;
    string Name;
    .... (other properties)
}


List<long> lstIDsEntitiesB; //this list has many IDs of entities B.

I would like to get all the entities A which property lstEntitiesB has one or more entities which ID is on lstIDsEntitiesB.

I don't know if I have to use a join or there are any other way, perhaps with any or contains.

Thanks so much.


Solution

  •     class MyEntityA
        {
            public long IDEntityA;
            public List<MyEntityB> lstEntityB;
        }
    
        class MyEntityB
        {
            public long IDEntityB;
            public string Name;
        }
    
        public class Test
        {
            List<long> lstIDsEntitiesB; 
    
            public void TestAlvaroProblem()
            {
                List<MyEntityA> entitiesA = new List<MyEntityA>();
                IEnumerable<MyEntityA> filteredOut = entitiesA.Where(a => a.lstEntityB
                                .Select(b => b.IDEntityB).Intersect(lstIDsEntitiesB).Any());
            }        
        }
    

    You should select entitiesA where lstEntityB's Ids intersect with lstIDsEntitiesB