-4

I have a requirement where I need to group records. For example I have 150 records and I need to get them in 5 groups, 30 records in each group.

Can anyone let me know how can I accomplish this?

Thanks in advance.

nrsharma
  • 2,532
  • 3
  • 20
  • 36
  • 2
    What does your data structure of the records you need to group look like? Do you need to group indiscriminately (i.e 1-30, 31-60, 61-90, etc) or do you have a field you want to group by? – pieperu Nov 21 '16 at 12:36
  • How do you decide to create a new group? What if you have 10 items? How many groups you want in this case? It's not clear what you want to achieve.. – Dieterg Nov 21 '16 at 12:37
  • 2
    GroupBy is the way to go. Want a more elaborate answer? Write a more elaborate question – Alex Nov 21 '16 at 12:37
  • Show us some code. From what I read I'd say you can use Linqs Skip() & Take() methods. – Carra Nov 21 '16 at 12:37
  • @pieperu Ues I need to group by indiscriminately . – nrsharma Nov 21 '16 at 12:38
  • I see there are plenty of good suggestions in here now – pieperu Nov 22 '16 at 15:33

5 Answers5

0
var groups = myRecords.Select((e, i) => new { Item = e, Grouping = (i / 5) }).GroupBy(e => e.Grouping);
Marcel
  • 954
  • 8
  • 22
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Lynn Crumbling Nov 21 '16 at 17:56
0
var g1 = records.Take(30);
var g2 = records.Skip(30).Take(30);
...

If numbers 150 and 30 are just an example then you can easily put Skip(pageIndex*pageSize).Take(pageSize) in a loop.

tymtam
  • 31,798
  • 8
  • 86
  • 126
0

You can also do it easily with MoreLinq:

var items = list.Batch(30);
Carra
  • 17,808
  • 7
  • 62
  • 75
0

If there's no specific group by property and just want them in specific size then this:

static class LinqExtensions
{
    public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
    {
        int i = 0;
        var splits = from item in list
                     group item by i++ % parts into part
                     select part.AsEnumerable();
        return splits;
    }
}

Thanx to Hassan Kahn

Community
  • 1
  • 1
SJFJ
  • 657
  • 6
  • 18
0

suppose you have a list name EmployeeList having different departments. so you need to group them by Employee Department

       public class Employee
      {
        public string Name { get; set; }
        public string Department { get; set; }

      }
    public class EmployeeDepartMentList
    {
        public string DepartMentName {get;set; }
        public List<Employee> EmployeeList { get; set; }

    }
    public class FinalResult
    {
        public List<EmployeeDepartMentList> GetEmployeesByDepartment()
        {
            List<Employee> EmployeeList = new List<Employee>();

            var Model = EmployeeList.Select(x => x.Department).Distinct().Select(x => new EmployeeDepartMentList
            {
                DepartMentName = x,
                EmployeeList = EmployeeList.Where(y => y.Department == x).ToList()

            }).ToList();
            return Model;
        }

    }