0

My class

class student   
{
 public string studentid { get; set; }
 public string groupid { get; set; }//Group id
}

My List

List<student> pupils = new List<student>();

here I select students with no group ID. I have assigned students with no group ID as ="00"

      var studentWithNogroupID = from student in pupils where student.groupid == "00" select student;

I have an integer which I'm gonna increase each time and convert to char and assign each student.

int groupid=001;
string grid = myInt.ToString(groupid);

Here I'm assigning each member with group id like this..

foreach(var student in studentsWithNoGroupId)
{
  student.groupid = "grid";
}

My problem is I have to assign three members to a group. In other words 3 members should have same group id. And after each 3 members it should increase the group id which is like this to create a new group id.

 groupid+=1;

How to go three by three in for loop or can anyone provide me a solution to assign three students to a group.

sloth
  • 99,095
  • 21
  • 171
  • 219
user3572467
  • 59
  • 1
  • 1
  • 8
  • student.groupid = groupid / 3 and then groupid++ would that work? – Dennis_E May 23 '14 at 11:32
  • I'm not sure I understand what you are asking. Are you asking how to group `student` objects in groups of 3 students each? – Panagiotis Kanavos May 23 '14 at 11:32
  • This *is* a duplicate but the best answer can be found in http://stackoverflow.com/questions/13731796/create-batches-in-linq . You can use the Batch operator in [MoreLINQ](http://code.google.com/p/morelinq/) to batch elements in batches of 3. – Panagiotis Kanavos May 23 '14 at 11:38
  • @PanagiotisKanavos Yes groups of 3 students, But after each 3 students it should increase group id.. – user3572467 May 23 '14 at 11:53
  • @sloth Asked because I don't have proper knowledge in linq and querying. Please try to provide some code samples. i can't grab a single thing from batch listing – user3572467 May 23 '14 at 12:26
  • @user3572467 Take a look at [this](http://stackoverflow.com/a/419063/142637) answer. It splits a list into a list of lists of three elements. Then you could simply use something like `forach(var grp in Split(studentsWithNoGroupId) { foreach(var std in grp) { std.groupid == groupid; } groupid+=1; } ` – sloth May 23 '14 at 12:34

1 Answers1

1

Use a modulo operator to determine every third member:

for(int i = 0; i < studentsWithNoGroupId.Count; i++)
{
  if((i % 3) == 0)
   groupid += 1;
  studentsWithNoGroupId.ElementAt(i).groupId = groupid;
}
Edin
  • 1,476
  • 11
  • 21