Getting a Generic Collection from a Generic Collection C# Linq
-
I have a Generic collection of Student in StudentCollection Class and each Student Class is having a SubjectCollection Class. I have a method which returns the StudentCollection.How can I get the SubjectCollection directly from the StudentCollection ? I found two ways of doing it--- First Way StudentSubjectCollection studentsubjectCollection = new StudentSubjectCollection(); var studentsColl = GetAllStudentCollection(); foreach (var subject in studentsColl ) { studentsubjectCollection = subject.StudentSubjectCollection; } Second Way StudentSubjectCollection studentsubjectCollection = new StudentSubjectCollection(); StudentCollection studentColl = GetAllStudentCollection(); foreach (MessageModule messageModule in messageModuleCollection) { studentsubjectCollection.AddRange(messageModule.MessageModuleReplyCollection); } Is there any other way of doing this such as one line of code to get the StudentSubjectCollection directly from the StudentCollection. Something like StudentSubjectCollection studentsubjectColl = GetAllStudentCollection().Select(field=>field.....); but this doesnt work. Thanks --Vinay
-
I have a Generic collection of Student in StudentCollection Class and each Student Class is having a SubjectCollection Class. I have a method which returns the StudentCollection.How can I get the SubjectCollection directly from the StudentCollection ? I found two ways of doing it--- First Way StudentSubjectCollection studentsubjectCollection = new StudentSubjectCollection(); var studentsColl = GetAllStudentCollection(); foreach (var subject in studentsColl ) { studentsubjectCollection = subject.StudentSubjectCollection; } Second Way StudentSubjectCollection studentsubjectCollection = new StudentSubjectCollection(); StudentCollection studentColl = GetAllStudentCollection(); foreach (MessageModule messageModule in messageModuleCollection) { studentsubjectCollection.AddRange(messageModule.MessageModuleReplyCollection); } Is there any other way of doing this such as one line of code to get the StudentSubjectCollection directly from the StudentCollection. Something like StudentSubjectCollection studentsubjectColl = GetAllStudentCollection().Select(field=>field.....); but this doesnt work. Thanks --Vinay
It is not quite clear to me what you are trying to do. In the first way, you will end up with
studentsubjectCollection
equal to the subject collection of the last item instudentsColl
. In the second, you are getting the student collection, but not using it and instead pulling from another collection all together. Your final approach looks like it returns anIEnumerable<StudentSubjectCollection>
. If you want to flatten these collections into a single sequence, I would direct you to theSelectMany
method. When using the query syntax, this is what is called when you have multiple from statements, like so://assumes StudentSubjectCollection has a constructor that
//takes IEnumerable<StudentSubjectCollection>
var studentSubjectColl = new StudentSubjectCollection(
from stu in GetAllStudentCollection()
from subj in stu.StudentSubjectCollection
select subj);PS: Next time use the 'code block' button to preserve formatting in your code.
-
It is not quite clear to me what you are trying to do. In the first way, you will end up with
studentsubjectCollection
equal to the subject collection of the last item instudentsColl
. In the second, you are getting the student collection, but not using it and instead pulling from another collection all together. Your final approach looks like it returns anIEnumerable<StudentSubjectCollection>
. If you want to flatten these collections into a single sequence, I would direct you to theSelectMany
method. When using the query syntax, this is what is called when you have multiple from statements, like so://assumes StudentSubjectCollection has a constructor that
//takes IEnumerable<StudentSubjectCollection>
var studentSubjectColl = new StudentSubjectCollection(
from stu in GetAllStudentCollection()
from subj in stu.StudentSubjectCollection
select subj);PS: Next time use the 'code block' button to preserve formatting in your code.
'Select many' , yeah got it. Thanks.
StudentCollection studentColl = GetAllStudentCollection();
StudentSubjectCollection subjectColl = studentColl.SelectMany(field=>field.StudentSubjectCollection);