开发者

assign to a generic collection from a generic collection inside collection + linq + C#

开发者 https://www.devze.com 2023-01-10 05:45 出处:网络
I have a Generic collection of Student in StudentCollection Class and each Student Class is having a SubjectCollection Class.

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(mess开发者_StackOverflowageModule.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


var students = GetAllStudentCollection();
var subjects = students.SelectMany(stu=>stu.StudentSubjectCollection);
0

精彩评论

暂无评论...
验证码 换一张
取 消