Note :This post is first published on Mar-2013 in my previous blog Techkindle. Moving the content here.

There may be certain situations, When we have a number of objects in the list, but we need to split the objects into batches by batch size.

To achieve that we can use Skip and Take static methods in LINQ,

  • Skip : Bypasses a specified number of elements in a sequence and then returns the remaining elements.
  • Take : Returns a specified number of contiguous elements from the start of a sequence.

Using both these static methods, we can split the list into multiple lists based on the batch size.

While writing the source code, my main intention is to just show how to split the list into batch sizes. So didn’t take the validations, and other things into consideration.

  1. First of all creating a Student Class. Which is a template to create the Student objects. (We can use anonymous types also).
  2. Then writing a method to create a list of objects for student class. I have just given some random data, and creating some 23 objects.
  3. Then writing a method to split the passed list of objects into batches, based on the passed batch size.
  4. At last calling all the methods from the Main method. Here I am giving the full source code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SplitClass
{
public class Program
{
static void Main(string[] args)
{
int batchSize;// = 5;
Split objSplit = new Split();
List<Student> lstStudent = objSplit.CreateListOfStudents();
Console.WriteLine("Number of Students: = " + lstStudent.Count);
Console.WriteLine("Please Enter Batch Size: ");
batchSize = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("BatchSize: = " + batchSize);
List<List<Student>> lstStudentInBatches = objSplit.SplitIntoBatches(batchSize, lstStudent);
Console.WriteLine("Number of Batches: = " + lstStudentInBatches.Count);
if (lstStudentInBatches.Count > 0)
Console.Write("LastBatch Count : " + lstStudentInBatches[lstStudentInBatches.Count – 1].Count);
Console.ReadKey();
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Course { get; set; }
public Student(int id, string name, int age, string course)
{
Id = id;
Name = name;
Age = age;
Course = course;
}
}
public class Split
{
public List<Student> CreateListOfStudents()
{
List<Student> lstStudent = new List<Student>();
lstStudent.Add(new Student(1, "Sharath", 23, Course.BCA.ToString()));
lstStudent.Add(new Student(2, "Srikanth", 25, Course.BCA.ToString()));
lstStudent.Add(new Student(3, "Bharath", 25, Course.BCA.ToString()));
lstStudent.Add(new Student(4, "Ramu", 22, Course.BCA.ToString()));
lstStudent.Add(new Student(5, "Kiram", 25, Course.BCA.ToString()));
lstStudent.Add(new Student(6, "Vikram", 25, Course.BCA.ToString()));
lstStudent.Add(new Student(7, "Raju", 25, Course.BCA.ToString()));
lstStudent.Add(new Student(8, "Salman", 21, Course.BCA.ToString()));
lstStudent.Add(new Student(9, "Roshan", 25, Course.BCA.ToString()));
lstStudent.Add(new Student(10, "Chethan", 25, Course.BCA.ToString()));
lstStudent.Add(new Student(11, "Nikil", 25, Course.MCA.ToString()));
lstStudent.Add(new Student(12, "Divya", 26, Course.MCA.ToString()));
lstStudent.Add(new Student(13, "Prema", 25, Course.MCA.ToString()));
lstStudent.Add(new Student(14, "Rajesh", 25, Course.MCA.ToString()));
lstStudent.Add(new Student(15, "Kishore", 24, Course.MCA.ToString()));
lstStudent.Add(new Student(16, "Srinu", 25, Course.BE.ToString()));
lstStudent.Add(new Student(17, "Ramesh", 23, Course.BE.ToString()));
lstStudent.Add(new Student(18, "Mahesh", 21, Course.BE.ToString()));
lstStudent.Add(new Student(19, "Venky", 25, Course.BE.ToString()));
lstStudent.Add(new Student(20, "Krishna", 20, Course.BE.ToString()));
lstStudent.Add(new Student(21, "Shankhar", 21, Course.BE.ToString()));
lstStudent.Add(new Student(22, "Uday", 25, Course.BE.ToString()));
lstStudent.Add(new Student(23, "Murali", 20, Course.BE.ToString()));
return lstStudent;
}
public List<List<Student>> SplitIntoBatches(int batchSize, List<Student> lstStudent)
{
List<List<Student>> lstStudentsInBatches = new List<List<Student>>();
// Determine how many lists are required
int numberOfLists = (lstStudent.Count / batchSize);
int remainder = (lstStudent.Count % batchSize);
if (remainder > 0)
numberOfLists += 1;
for (int i = 0; i < numberOfLists; i++)
{
List<Student> newList = lstStudent.Skip(i * batchSize).Take(batchSize).ToList();
lstStudentsInBatches.Add(newList);
}
return lstStudentsInBatches;
}
enum Course
{
MCA, BCA, BE,
}
}
}

view raw
SplitToBatches
hosted with ❤ by GitHub

Output:

Happy Learning 🙂