Splitting an array using LINQ

前端 未结 7 1153
你的背包
你的背包 2020-12-10 15:02

I have a collection uni-dimensional like this:

[1,2,4,5.....n]

I would like to convert that collection in a bi-dimensional collection like

相关标签:
7条回答
  • 2020-12-10 15:49

    It's not a pure LINQ but it's intended to be used with it:

    public static class MyEnumerableExtensions
    {
        public static IEnumerable<T[]> Split<T>(this IEnumerable<T> source, int size)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source can't be null.");
            }
    
            if (size == 0)
            {
                throw new ArgumentOutOfRangeException("Chunk size can't be 0.");
            }
    
            List<T> result = new List<T>(size);
            foreach (T x in source)
            {
                result.Add(x);
                if (result.Count == size)
                {
                    yield return result.ToArray();
                    result = new List<T>(size);
                }
            }
        }
    }
    

    It can be used from your code as:

    private void Test()
    {
        // Here's your original sequence
        IEnumerable<int> seq = new[] { 1, 2, 3, 4, 5, 6 };
    
        // Here's the result of splitting into chunks of some length 
        // (here's the chunks length equals 3). 
        // You can manipulate with this sequence further, 
        // like filtering or joining e.t.c.
        var splitted = seq.Split(3);
    }
    
    0 讨论(0)
  • 2020-12-10 15:50

    I based my solution of Jeremy Holovacs's answer and used Take() and Skip() to create subarrays.

            const int batchSize = 3;
            int[] array = new int[] { 1,2,4,5.....n};
    
            var subArrays = from index in Enumerable.Range(0, array.Length / batchSize + 1)
                          select array.Skip(index * batchSize).Take(batchSize);
    
    0 讨论(0)
  • 2020-12-10 15:58

    Use MoreLinq.Batch

     var result = inputArray.Batch(n); // n -> batch size
    

    Example

        var inputs = Enumerable.Range(1,10);
    
        var output = inputs.Batch(3);
    
    
        var outputAsArray = inputs.Batch(3).Select(x=>x.ToArray()).ToArray(); //If require as array
    
    0 讨论(0)
  • 2020-12-10 15:58

    The sample below will split an array into groups of 4 items each.

    int[] items = Enumerable.Range(1, 20).ToArray(); // Generate a test array to split
    int[][] groupedItems = items
                             .Select((item, index) => index % 4 == 0 ? items.Skip(index).Take(4).ToArray() : null)
                             .Where(group => group != null)
                             .ToArray();
    
    0 讨论(0)
  • 2020-12-10 16:02

    You can group by the index divided by the batch size, like this:

    var batchSize = 3;
    var batched = orig
        .Select((Value, Index) => new {Value, Index})
        .GroupBy(p => p.Index/batchSize)
        .Select(g => g.Select(p => p.Value).ToList());
    
    0 讨论(0)
  • 2020-12-10 16:02

    It's as simple as:

    static class LinqExtensions
    {
        public static IEnumerable<IEnumerable<T>> ToPages<T>(this IEnumerable<T> elements, int pageSize)
        {
            if (elements == null)
                throw new ArgumentNullException("elements");
            if (pageSize <= 0)
                throw new ArgumentOutOfRangeException("pageSize","Must be greater than 0!");
    
            int i = 0;
            var paged = elements.GroupBy(p => i++ / pageSize);
            return paged;
        }
    }
    
    0 讨论(0)
提交回复
热议问题