Skip to content

Instantly share code, notes, and snippets.

@gopigujjula
Created March 15, 2013 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gopigujjula/5169877 to your computer and use it in GitHub Desktop.
Save gopigujjula/5169877 to your computer and use it in GitHub Desktop.
This sample application, will create a number of objects , and add those to the list. But based on the specified batch size, it will split the list into number of batches.
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,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment