Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Getting a Generic Collection from a Generic Collection C# Linq

Getting a Generic Collection from a Generic Collection C# Linq

Scheduled Pinned Locked Moved LINQ
csharpquestionlinq
3 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinay Varghese
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • V Vinay Varghese

      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

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      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 in studentsColl. 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 an IEnumerable<StudentSubjectCollection>. If you want to flatten these collections into a single sequence, I would direct you to the SelectMany 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.

      V 1 Reply Last reply
      0
      • G Gideon Engelberth

        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 in studentsColl. 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 an IEnumerable<StudentSubjectCollection>. If you want to flatten these collections into a single sequence, I would direct you to the SelectMany 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.

        V Offline
        V Offline
        Vinay Varghese
        wrote on last edited by
        #3

        'Select many' , yeah got it. Thanks.

        StudentCollection studentColl = GetAllStudentCollection();
        StudentSubjectCollection subjectColl = studentColl.SelectMany(field=>field.StudentSubjectCollection);

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups