简体   繁体   中英

Filtering generic collection while comparing with another collection

I have to do some filtering in a generic collection. I am trying to use LINQ for that. Here is my code:

from student in students
where student.ID == (Here is another collection) from newstudent in Newstudents
select newstudent.ID
select student 

I don't know how to compare int collection with single int. Please tell me a good way to do it.

For fast lookup first put the student id's in a HashSet . The use Contains on that for checking existence of the id.

var studentIds = new HashSet<int>(newStudents.Select(x => x.ID));
var filtered = students.Where(x => studentIds.Contains(x.ID));

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