Group the repetitive value in the object list using Linq query.
I have the following data as table called "SudentAssessment" with these data.
AssessmentId Username SITSupervisor WorkSupervisor
1 iwsp.student001 iwsp.staff001 iwsp.supervisor001
2 iwsp.student001 iwsp.staff002 iwsp.supervisor001
3 iwsp.student002 iwsp.staff001 iwsp.supervisor002
4 iwsp.student003 iwsp.staff003 iwsp.supervisor003
5 iwsp.student004 iwsp.staff001 iwsp.supervisor004
6 iwsp.student004 iwsp.staff005 iwsp.supervisor004
7 iwsp.student005 iwsp.staff003 iwsp.supervisor005
Here the issue is row number 1,2 and 5,6 having same data but only difference is SIT supervisor detail is different. These each rows populated into StudentAssessmentDTO which looks like below.
public class StudentAllocationDTO
{
public int AssessmentId {get;set;}
public string Username {get;set;}
public string SITSupervisor {get;set;}
public string WorkSupervisor {get;set;}
}
As per the current implementation I when call a method that returns List with all 7 records. as row 1,2 and 5,6 has more only difference in "SITSupervisor" I want to assign to the below DTO structure using LINQ in c#.
public class NEWStudentAllocationDTO
{
public int AssessmentId {get;set;}
public string Username {get;set;}
public List<string> SITSupervisor {get;set;}
public string WorkSupervisor {get;set;}
}
If you anyone need further clarification please let me know in the comment.
.GroupBy(c => new {AssessmentId = c.AssessmentId, Username = c.Username}). After grouping you can loop through the items and instanciate your NEWStudentAllocationDTOAssessmentIdis an issue, cause it is an property of yourNewStudentAllocationDTOclass.