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

  • Question

  • I have this entities:

        long IDEntityA;
        List<EntityB> lstEntityB;
        .... (other properties);
        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.

    Thursday, April 10, 2014 9:46 AM


  • Hello,

    Have a try to use Any() method as:

    List<int> OrderDetailIDs = new List<int>() { 1, 2, 3};
                using (DFDBEntities db = new DFDBEntities())
                    var result = db.Orders.Where(o => o.OrderDetails.Any(od => OrderDetailIDs.Contains(od.OrderDetailID))).ToList();

    The Order and OrderDetail are one to many relationship just the same as yours.

    You can have a try, if this does not work for you, please let me know.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 11, 2014 7:42 AM