Home >Backend Development >C++ >How Can LINQ Be Used to Split a List into Sublists Based on Item Index?
Divide the list using LINQ into a subjistist
There is a large number of lists, and you want to split it into smaller and easier to handle. LINQ (Language Integrated Query) is a powerful tool that can easily achieve this.Assignment:
How do you use the item index as a separate character and divide List
Solution:
The custom functions using LINQ to execute this task are shown below.
<code class="language-csharp">public static List<List<T>> Split<T>(IList<T> source) { return source .Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / 3) .Select(x => x.Select(v => v.Value).ToList()) .ToList(); }</code>
Explanation:
In the provided example, [a, g, e, w, p, s, q, f, f, f, y, I, m, c, c, c, c, c, c, It is divided into g, e], [w, p, s], [q, f, x], [y, i, m], [c]].
The above is the detailed content of How Can LINQ Be Used to Split a List into Sublists Based on Item Index?. For more information, please follow other related articles on the PHP Chinese website!