0

I have a list of n objects (17 for now) and I wanted to know if it is possible to take said list and split it into (potentially) 2 groups. That way the end result would be

NewList
  -"GroupA"
    -List1 = {"john", "mary", "sam"}

  -"GroupB"
    -List2 = {"tony", "aaron"}

The desired result would help me output the first half of the list of students in page 1 and then using paging the user can then view the remaining list on the next page.

Right now I am trying to do something like this:

var groupList = Classroom.GroupBy(o => o).Select(grp=>grp.Take((Classroom.Count + 1) / 2)).ToList();

But when I debug it I'm still getting the full list. Can it be done via linq?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
user2529011
  • 705
  • 3
  • 11
  • 21

1 Answers1

0

You can create group by some property. For example, we have 50 students, then we can make GroupId property and group them by GroupId property:

var students = new List<Student>();
for (int i = 0; i < 50; i++)
{
    students.Add(new Student { Id = i, Name = $"Student { i }" });
}

var sectionedStudents = students.Select(s => new
{
     GroudId = s.Id / 10,
     s.Id,
     s.Name
});
var groupedStudents = sectionedStudents.GroupBy(s => s.GroudId);

and Person class:

class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148