[英]Filtering generic collection while comparing with another collection
我必須在通用集合中進行一些過濾。 我正在嘗試使用LINQ。 這是我的代碼:
from student in students
where student.ID == (Here is another collection) from newstudent in Newstudents
select newstudent.ID
select student
我不知道如何將int集合與單個int進行比較。 請告訴我一個好方法。
為了快速查找,首先將學生ID放入HashSet
。 用途Contains
在此用於檢查id的存在。
var studentIds = new HashSet<int>(newStudents.Select(x => x.ID));
var filtered = students.Where(x => studentIds.Contains(x.ID));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.