C# List Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
Here are some examples of splitting lists into sub-lists in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int chunkSize = 3; var chunks = Enumerable.Range(0, (int)Math.Ceiling(numbers.Count / (double)chunkSize)) .Select(i => numbers.Skip(i * chunkSize).Take(chunkSize).ToList()) .ToList(); foreach (var chunk in chunks) { Console.WriteLine(string.Join(", ", chunk)); }
In this example, a List<int>
is initialized with several integer values, and a chunkSize
variable is set to 3. The Enumerable.Range
method is used to create a range of integers from 0 to the number of chunks needed to cover the list, rounded up to the nearest integer. The Select
method is then used to select each chunk of the list using the Skip
and Take
methods to skip the appropriate number of elements and take the next chunk of the specified size. Finally, the resulting list of chunks is printed to the console using a foreach
loop.
List<string> words = new List<string> { "apple", "banana", "orange", "pear", "grape" }; int maxLength = 4; var subLists = words.GroupBy(word => word.Length <= maxLength) .ToDictionary(group => group.Key, group => group.ToList()); foreach (var subList in subLists) { Console.WriteLine("Length {0}:", subList.Key ? "<=" + maxLength : ">" + maxLength); Console.WriteLine(string.Join(", ", subList.Value)); }
In this example, a List<string>
is initialized with several string values, and a maxLength
variable is set to 4. The GroupBy
method is used to group the strings based on whether their length is less than or equal to the maximum length, resulting in two groups: one for strings with length less than or equal to maxLength
, and one for strings with length greater than maxLength
. The ToDictionary
method is then used to create a dictionary of the sub-lists, with the key being a boolean value indicating whether the length of the strings in the sub-list is less than or equal to maxLength
. Finally, the resulting sub-lists are printed to the console using a foreach
loop.
List<int> numbers = new List<int> { 1, 2, 3, 1, 2, 4, 5, 3, 2, 6 }; List<int> subListValues = new List<int> { 1, 2, 3 }; var subLists = new List<List<int>>(); int index = 0; while (index < numbers.Count) { int subListIndex = numbers.IndexOf(subListValues[0], index); if (subListIndex == -1) break; bool isSubList = true; for (int i = 1; i < subListValues.Count; i++) { if (subListIndex + i >= numbers.Count || numbers[subListIndex + i] != subListValues[i]) { isSubList = false; break; } } if (isSubList) { subLists.Add(numbers.Skip(index).Take(subListIndex - index).ToList()); index = subListIndex + subListValues.Count; } else { index++; } } if (index < numbers.Count) { subLists.Add(numbers.Skip(index).ToList()); } foreach (var subList in subLists) { Console.WriteLine(string.Join(", ", subList)); }
In this example, a List<int>
is initialized with several integer values, and a List<int>
is initialized with the values of the sub-list to search for. The subLists
variable is initialized as an empty list to hold the resulting sub-lists. A while
loop is used to search for each occurrence of the sub-list, starting from the current index
. The IndexOf
method is used to find the index of the first value of the sub-list, and if it is found, a nested loop is used to check if the sub-list is present at that position. If the sub-list is found, the corresponding sub-list of values is added to the subLists
list, and the index
is updated to skip past the sub-list. If the sub-list is not found, the index
is incremented and the loop continues. Finally, the resulting sub-lists are printed to the console using a foreach
loop.
C# split list into sublists:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> sublists = SplitList(numbers, 3); // Custom method to split list into sublists public static List<List<T>> SplitList<T>(List<T> source, int chunkSize) { return source .Select((value, index) => new { Index = index, Value = value }) .GroupBy(item => item.Index / chunkSize) .Select(chunk => chunk.Select(item => item.Value).ToList()) .ToList(); }
C# split list into batches:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> batches = SplitList(numbers, 5);
C# partition list into equal parts:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> partitions = PartitionList(numbers, 3); // Custom method to partition list into equal parts public static List<List<T>> PartitionList<T>(List<T> source, int partitionCount) { int itemsPerPartition = (int)Math.Ceiling((double)source.Count / partitionCount); return source .Select((value, index) => new { Index = index, Value = value }) .GroupBy(item => item.Index / itemsPerPartition) .Select(partition => partition.Select(item => item.Value).ToList()) .ToList(); }
Split List into groups with LINQ in C#:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> groups = numbers.GroupBy((item, index) => index / 3).Select(group => group.ToList()).ToList();
C# split list into chunks of n elements:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> chunks = numbers.Select((value, index) => new { Value = value, Index = index }) .GroupBy(x => x.Index / 3) .Select(group => group.Select(x => x.Value).ToList()) .ToList();
C# split list into parts with specific criteria:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> parts = numbers.GroupBy(n => n % 2 == 0).Select(group => group.ToList()).ToList();
Split List into sublists using foreach loop in C#:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> sublists = new List<List<int>>(); foreach (int number in numbers) { if (sublists.Count == 0 || sublists.Last().Count == 3) { sublists.Add(new List<int>()); } sublists.Last().Add(number); }
Split List into sublists with LINQ GroupBy in C#:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> sublists = numbers.GroupBy((item, index) => index / 3).Select(group => group.ToList()).ToList();
Split List into chunks with remainder in C#:
List<int> numbers = new List<int> { /* List of integers */ }; int chunkSize = 3; int remainder = numbers.Count % chunkSize; List<List<int>> chunks = numbers .Select((value, index) => new { Value = value, Index = index }) .GroupBy(x => x.Index / chunkSize) .Select(group => group.Select(x => x.Value).ToList()) .ToList(); if (remainder > 0) { // Handle remainder List<int> lastChunk = chunks.Last(); lastChunk.AddRange(numbers.Skip(lastChunk.Count)); }
Split List into sublists with specific element criteria in C#:
List<int> numbers = new List<int> { /* List of integers */ }; List<List<int>> sublists = new List<List<int>>(); int threshold = 5; foreach (int number in numbers) { if (sublists.Count == 0 || sublists.Last().Count >= threshold) { sublists.Add(new List<int>()); } sublists.Last().Add(number); }
Split List into sublists using skip and take in C#:
List<int> numbers = new List<int> { /* List of integers */ }; int batchSize = 3; List<List<int>> sublists = new List<List<int>>(); for (int i = 0; i < numbers.Count; i += batchSize) { sublists.Add(numbers.Skip(i).Take(batchSize).ToList()); }
Split List into sublists with LINQ TakeWhile and SkipWhile in C#:
List<int> numbers = new List<int> { /* List of integers */ }; int batchSize = 3; List<List<int>> sublists = numbers .Select((value, index) => new { Value = value, Index = index }) .TakeWhile(x => x.Index % batchSize != 0) .GroupBy(x => x.Index / batchSize) .Select(group => group.Select(x => x.Value).ToList()) .ToList();