简体   繁体   中英

Efficient linq query to return objects when you have a list of object id's

I have a List of all my peronIDs and I'd like to run a Linq query that retrieves the remaining details.

This is my code, but I believe that it checks every item in my People db table against every item in my personID's list which is inefficient:

List<int> personIDs = Session["PeopleList"] as List<int>;

var people = (from c in db.People
    where (
    from a in personIDs
    where a == c.PersonID
    select a
    ).Any()
    select c);

Is there a more efficient way to run this code?

You can do it this way:

var people = (from c in db.People
              where personIDs.contains(c.id)
              select c).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM