c# - Linq to select all Children in all Classes -


i have list of classes each class has list of students.

i need full list of students, unique on student.personid

allpeopleunique=   classes.select(c=> c.students.select( persons ??)).unique(student.personid) 

any ideas?

you want use selectmany:

var allstudents = classes.selectmany(c => c.students).distinct(); 

if student objects single unique student not same, can use distinctby recipe question. or implement iequalitycomparer student type:

public class studentequalitycomparer : iequalitycomparer<student> {     public bool equals(student a, student b)     {         return a.personid == b.personid;     }     public int gethashcode(student s)     {         return s.personid.gethashcode(); // or `return s.personid`     } } 
var allstudents = classes.selectmany(c => c.students)                          .distinct(new studentequalitycomparer()); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -