If you want to split the generic list in to sub list in C# , one of the simplest way is to use LINQ (Language Integrated Query). Below is a code sample demonstrating this feature.
How to Split List into Sub list in C# using LINQ ?
using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace AbundantcodeApp { class Program { static void Main(string[] args) { List<string> strLst = new List<string>(); strLst.Add("Android Development"); strLst.Add("Windows Phone Development"); strLst.Add("Abundantcode Programming"); var result = Split(strLst); Console.ReadLine(); } public static List<List<string>> Split(List<string> input) { return input.Select((item1, index) => new { Index = index, Value = item1 }) .GroupBy(x => x.Index / 3) .Select(x => x.Select(v => v.Value).ToList()) .ToList(); } } }
Leave a Reply