I have an object allStudents = Dictionary<ClassRoom, List<Student>>()
In Linq how would I get a list of all the students who are male? (student.Gender=="m") from all the Classrooms?
Ian
Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.
C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).
Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.
Try the following
var maleStudents = allStudents
.SelectMany(x => x.Values)
.Where(x => x.Gender=="m");
The trick to this is the SelectMany
operation. It has the effect of flattening a collection of List<Student>
into a single collection of Student
. The resulting list is the same as if you'd lined up each list front to back.
You can use nested from
clause. The first from
selects all classes together with their students (an item from the dictionary), which is represented as a KeyValuePair<ClassRoom, List<Student>>
. Then you can select all students from the class using the Value
property and filter them:
var q = from cls in allStudents
from s in cls.Value
where s.Gender == "M" select s;
Under the cover, the nested from
clause is translated to the SelectMany
method call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With