Share via


Split List Into Chunks

Question

Tuesday, August 27, 2019 2:03 AM

Hi - how would I split this list into smaller chunks of 2?  

And also, once split, how would I access the individual chunks?

public static void DoSomething()
{
    List<String> testList = new List<String>();
    testList.Add("2");
    testList.Add("3");
    testList.Add("5");
    testList.Add("7");
    testList.Add("21");
    testList.Add("31");
    testList.Add("51");
    testList.Add("71");
    testList.Add("22");
    testList.Add("23");
    testList.Add("25");
    testList.Add("27");
    testList.Add("32");
    testList.Add("33");
    testList.Add("35");
    testList.Add("37");


    //split testList into smaller List<String> of 1 each

}

All replies (3)

Tuesday, August 27, 2019 2:49 AM âś…Answered

Hi IndigoMontoya,

Thank you for posting here.

You could try the following code to split testList  into smaller List<String>.

    class Program
    {
        static void Main(string[] args)
        {
            DoSomething();
            Console.ReadKey();
        }

        public static void DoSomething()
        {
            List<String> testList = new List<String>();
            testList.Add("2");
            testList.Add("3");
            testList.Add("5");
            testList.Add("7");
            testList.Add("21");
            testList.Add("31");
            testList.Add("51");
            testList.Add("71");
            testList.Add("22");
            testList.Add("23");
            testList.Add("25");
            testList.Add("27");
            testList.Add("32");
            testList.Add("33");
            testList.Add("35");
            testList.Add("37");
            var list = splitList(testList, 8);
            foreach (var item in list)
            {
                foreach (string i in item)
                {
                    Console.WriteLine(i);
                }
                Console.WriteLine("*****");
            }



            //split testList into smaller List<String> of 1 each

        }

        public static List<List<string>> splitList(List<string> locations, int nSize)
        {
            var list = new List<List<string>>();

            for (int i = 0; i < locations.Count; i += nSize)
            {
                list.Add(locations.GetRange(i, Math.Min(nSize, locations.Count - i)));
            }

            return list;
        }
    }

Result:

Best Regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Tuesday, August 27, 2019 2:31 AM

Greetings Indigo.

Your question is a little ambiguous, but maybe this is what you're after.

using System;
using System.IO;
using System.Collections.Generic;


namespace ConsoleApp
{
   class Program
   {
      static void Main(string[] args)
      {
         List<String> testList = new List<String>();
         testList.Add("2");
         testList.Add("3");
         testList.Add("5");
         testList.Add("7");
         testList.Add("21");
         testList.Add("31");
         testList.Add("51");
         testList.Add("71");
         testList.Add("22");
         testList.Add("23");
         testList.Add("25");
         testList.Add("27");
         testList.Add("32");
         testList.Add("33");
         testList.Add("35");
         testList.Add("37");

         TwoStringsList tsl = new TwoStringsList(testList);

         foreach (TwoStrings ts in tsl)
         {
            Console.WriteLine(ts.S1 + " " + ts.S2);
         }
      }
   }


   public class TwoStrings
   {
      public string S1 { get; private set; }
      public string S2 { get; private set; }

      public TwoStrings(string s1, string s2)
      {
         S1 = s1;
         S2 = s2;
      }
   }

   public class TwoStringsList : List<TwoStrings>
   {
      public TwoStringsList(List<string> list)
      {
         for (int i = 0; i < list.Count; i += 2)
         {
            string s1 = list[i];
            string s2 = "";
            if (i + 1< list.Count)
               s2 = list[i + 1];

            this.Add(new TwoStrings(s1, s2));

         }
      }
   }
   
}


Tuesday, August 27, 2019 2:55 AM

Or if want to write in Linq:

            List<List<string>> result = testList.Select((x, i) => new { rid = i, item = x })
            .GroupBy(x => x.rid / 2)
            .Select(g => g.Select(x => x.item).ToList()).ToList();

            foreach (var item in result)
            {
                Console.WriteLine("{0} {1}", item[0], item.Count > 1 ? item[1] : String.Empty);
            }