简体   繁体   English

当您有对象ID列表时,有效的linq查询返回对象

[英]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. 我有一个包含所有peronID的列表,我想运行一个Linq查询来检索剩余的详细信息。

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: 这是我的代码,但我相信它会检查我的People数据库表中的每个项目,而不是我的personID列表中的每个项目都是低效的:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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