[英]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.