[英]Linq query geting list by comparing Id with an list of objects that have ids
[英]Efficient linq query to return objects when you have a list of object id's
我有一個包含所有peronID的列表,我想運行一個Linq查詢來檢索剩余的詳細信息。
這是我的代碼,但我相信它會檢查我的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);
有沒有更有效的方法來運行此代碼?
你可以這樣做:
var people = (from c in db.People
where personIDs.contains(c.id)
select c).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.