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. C#
  4. How to set a property in a collection based on the join?

How to set a property in a collection based on the join?

Scheduled Pinned Locked Moved C#
questioncsharplinqtutorial
2 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    sri_3464 0
    wrote on last edited by
    #1

    i have two collections defined below

    List lstScienceStudents = GetScienceStudentCollection();
    List lstMathsStudents = GetMathsStudentCollection();

    Now both the classes ScienceStudents and MathsStudents have a common id Student_Id. I need to set the boolean property IsMathStudent in lstScienceStudents to true by joining the two collection. To be more clear here is the code

    for (int i = 0; i < lstScienceStudents .Count; i++)
    {
    for (int j = 0; j < lstMathsStudents.Count; j++)
    {
    if (lstScienceStudents[i].Student_Id == lstMathsStudents[j].Student_Id)
    lstScienceStudents[i].IsMathStudent = true;
    }
    }

    I know i can get the collection of MathsStudents in ScienceStudent collection using join in linq. But i need to set the flag to true in the lstScienceStudent collection list. How can i do it?

    D 1 Reply Last reply
    0
    • S sri_3464 0

      i have two collections defined below

      List lstScienceStudents = GetScienceStudentCollection();
      List lstMathsStudents = GetMathsStudentCollection();

      Now both the classes ScienceStudents and MathsStudents have a common id Student_Id. I need to set the boolean property IsMathStudent in lstScienceStudents to true by joining the two collection. To be more clear here is the code

      for (int i = 0; i < lstScienceStudents .Count; i++)
      {
      for (int j = 0; j < lstMathsStudents.Count; j++)
      {
      if (lstScienceStudents[i].Student_Id == lstMathsStudents[j].Student_Id)
      lstScienceStudents[i].IsMathStudent = true;
      }
      }

      I know i can get the collection of MathsStudents in ScienceStudent collection using join in linq. But i need to set the flag to true in the lstScienceStudent collection list. How can i do it?

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      You can't modify a field during a Linq query. But, you can return a new IEnuerable(T) or List(T) Something like this:

        var needed = from sRec in lstScienceStudents
               from mRec in lstMathsStudents
               where sRec.Student\_Id.Equals(mRec.Student\_Id)
               select new{
                   Field1here = YouKnowWhat;
                   Field2here = YouKnowWhat;
               }
      
      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