1

I have a code in C# that uses IGrouping, but I didn't do it and I don't know how to fix the error that is occurring. Here's the code:

public static class ListExtensions
{
   public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize) => source.Select((x, i) => new
   {
          Index = i,
          Value = x
   })
   .GroupBy(x => x.Index / chunkSize)
   .Select<IGrouping<int, \u003C\u003Ef__AnonymousType0<int, T>>, List<T>>(x => x.Select(v => v.Value).ToList<T>()).ToList<List<T>>();
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 3
    Looks like you've decompiled code that your decompiler can't handle. – ProgrammingLlama Jun 03 '22 at 13:25
  • 1
    There is already such a function in the framework. https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.chunk so you can just delete that funtion. – Magnus Jun 03 '22 at 13:26
  • 1
    `.Select>, List>` this part, this isn't quite right. diplomacynotwar is right, you need to "fix" this by understanding the context of the code. – Bagus Tesa Jun 03 '22 at 13:26
  • I think that was it too, unfortunately this code was passed to me by someone else and I need to correct it – Jadisson Amorim Jun 03 '22 at 13:27
  • 1
    @JadissonAmorim, try remove the `>` and let intellisense guides your fingers. also, i remember that `Chunk` think wasn't exists in 4.5 (or 4.6?) .net framework era. that might be the reason the previous dev need to write this kind of [stuff](https://stackoverflow.com/questions/12389203/how-do-i-chunk-an-enumerable). – Bagus Tesa Jun 03 '22 at 13:29
  • 2
    seems like a pretty pointless task. What did you do to deserve that? – MakePeaceGreatAgain Jun 03 '22 at 13:48

1 Answers1

5

If you are on .Net 6 you should use the built in Enumerable.Chunk method. If you are on an older version I would suggest an implementation that uses avoids using GroupBy to chunk the list, something like:

public static IEnumerable<List<T>> Chunk<T>(this IEnumerable<T> self, int chunkSize)
{
    var list = new List<T>(chunkSize);
    foreach (var item in self)
    {
        list.Add(item);
        if (list.Count >= chunkSize)
        {
            yield return list;
            list = new List<T>(chunkSize);
        }
    }
    if (list.Count > 0)
    {
        yield return list;
    }
}

While this is a little bit more code, it works with any IEnumerable, not just List. It will also be evaluated lazily, so will work fine even if the source sequence is infinite.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • But why when there already [exists such a function](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.chunk?view=net-6.0)? – Magnus Jun 03 '22 at 13:41
  • @Magnus does `Chunk` exists on .net 4.8? that link keep giving me `&viewFallbackFrom=netframework-4.8` thing and redirects to .net core 6 for some reason. – Bagus Tesa Jun 03 '22 at 13:45
  • @Magnus I'm still stuck in .Net 4.8, so thanks for the link, I have added it to the answer. I think my example would still be useful for anyone stuck on an older version. – JonasH Jun 03 '22 at 13:48
  • 1
    @Bagus Tesa the page redirects since the method was added in .net 6. If you are on an older version you need to provide your own implementation. – JonasH Jun 03 '22 at 13:50
  • No, only >= 6. But the implementation can be found [here](https://source.dot.net/#System.Linq/System/Linq/Chunk.cs,577032c8811e20d3): – Magnus Jun 03 '22 at 13:52