3

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.

5
  • 2
    I don't understand your question, what is the result you are expecting? you want to group them? Commented Jan 30, 2019 at 7:21
  • 1
    If you take the rows 1 and 2. Which AssessmentId should be taken? What should happen if the WorkSupervisor differs for the same student? Commented Jan 30, 2019 at 7:22
  • You can use .GroupBy(c => new {AssessmentId = c.AssessmentId, Username = c.Username}). After grouping you can loop through the items and instanciate your NEWStudentAllocationDTO Commented Jan 30, 2019 at 7:22
  • @Oliver AssessmentId will not be an issue here. I want to populate the results to just to showcase in the Jquery table. Commented Jan 30, 2019 at 7:27
  • 1
    AssessmentId is an issue, cause it is an property of your NewStudentAllocationDTO class. Commented Jan 30, 2019 at 7:32

1 Answer 1

4

Group them by an anonymous type containing the common properties.

        IEnumerable< NEWStudentAllocationDTO> grouped = l.GroupBy(x => new { x.Username, x.WorkSupervisor })
            .Select(x => new NEWStudentAllocationDTO()
            {
                AssessmentId = x.Key.AssessmentId,
                WorkSupervisor = x.Key.WorkSupervisor,
                Username = x.Key.Username,
                SITSupervisor = x.Select(y => y.SITSupervisor).ToList()
            });
Sign up to request clarification or add additional context in comments.

5 Comments

You should remove the AssessmentId from the anonymous type. Otherwise nothing will be grouped (according to the given example data).
This is true. I left it as I was wondering that the type NEWStudentAllocationDTO also contains such field, not as list. EDIT2: I remove it.
That was my biggest issue, too.
@Malior just understood what was requested reading your answer, the x.Select(y => y.SITSupervisor).ToList() may return the list of SITSupervisors as I understood? Interesting I didn't know it was possible :)
Thanks @Malior for the answer. It works well for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.