How to set a property in a collection based on the join?
-
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?
-
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?
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; }