0

I have a list of something.

public List<Objects> obj;

The objects in this list need to be added to these other lists.

public List<Objects> objGroup1, objGroup2, objGroup3, objGroup4; 

I assign them right now by doing this.

void AssignToGroups()
{
    for(int i = 0; i < obj.Count ; i++)
    {
//Need the first 4 for group 1 next 4 for group 2 and so on...

        if(i < 4)
        {
            objGroup1.Add(obj[i]);
        }

        else if(i >= 4 && i < 8)
        {
            objGroup2.Add(obj[i]);
        }

        else if (i >= 8 && i < 12)
        {
            objGroup3.Add(obj[i]);
        }

        else if (i >= 12 && i < 16)
        {
            objGroup4.Add(obj[i]);
        }
    }
}

I'm planning on expanding and my method for grouping objects right now will fill my screen with endless if and else statements.

4 objects need to be assigned to each groups. The objects in the group gets them in their order of arrangement. e.g. group1 gets obj 1-4. group 2 get obj 5-8 and so on...

Does anyone have a better method of grouping objects like this?

2 Answers2

3

You can utilize the Skip and Take methods.

You'll need the using System.Linq;:

objGroup1 = obj.Take(4).ToList();           //edited: use ToList() to keep the list format
objGroup2 = obj.Skip(4).Take(4).ToList();
objGroup3 = obj.Skip(8).Take(4).ToList();    
objGroup4 = obj.Skip(12).Take(4).ToList();
objGroup5 = obj.Skip(16).Take(4).ToList();

Let me know if it works, since I am not able to test it now, except for the syntax.

You can also group the obj before Take(), such as

var orderedobj = obj.OrderBy(i => "some order criteria").ToList();
objGroup1 = orderedobj.Take(4);
...

I referenced my answer on How to get first N elements of a list in C#?.

EDIT:

In case you somehow do not want to use Linq, you can also use GetRange

objGroup1 = obj.GetRange(0, 4);
objGroup2 = obj.GetRange(3, 4);     //since GetRange(index, count) has index starting from 0 instead of 1
objGroup3 = obj.GetRange(7, 4);     //count stays the same since we always want 4 elements
objGroup4 = obj.GetRange(11, 4);
objGroup5 = obj.GetRange(15, 4);
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
0

Using Keyur's excellent answer, you could create a method that will create the groups for you, based on any source list, with a configurable group size:

private static List<List<object>> AssignToGroups(List<object> source, int groupSize)
{
    var groups = new List<List<object>>();

    if (source == null || groupSize < 1) return groups;

    for (int i = 0; i < source.Count / groupSize; i++)
    {
        groups.Add(source.Skip(groupSize * i).Take(groupSize).ToList());
    }

    return groups;
}

Usage

private static void Main()
{
    var mainList = new List<object>
    {
        "one", "two", "three", "four","five",
        "six","seven","eight","nine","ten",
        "eleven", "twelve", "thirteen", "fourteen","fifteen",
        "sixteen","seventeen","eightteen","nineteen","twenty",
        "twentyone", "twentytwo", "twentythree", "twentyfour","twentyfive",
        "twentysix","twentyseven","twentyeight","twentynine","thirty",
        "thirtyone", "thirtytwo", "thirtythree", "thirtyfour","thirtyfive",
        "thirtysix","thirtyseven","thirtyeight","thirtynine","forty",
    };

    var groups = AssignToGroups(mainList, 4);

    for (var i = 0; i < groups.Count; i++)
    {
        Console.WriteLine($"Group #{i + 1}: {string.Join(", ", groups[i])}");
    }

    Console.WriteLine("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43