C# 資料分群

在這邊主要介紹List的分群,如有以下的五筆資料

1
["00001","00002","00003","00004","00005"]

這時想要以2來群分,變成下方結果

1
[["00001","00002"],["00003","00004"],["00005"]]

程式碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.Json;

namespace ListGroupBy
{
internal class Program
{
static void Main(string[] args)
{
var numbers = new List<string>
{
"00001","00002","00003","00004","00005"
};

var numberGroup = numbers
.Select((number, index) => new { number, index })
.GroupBy(g => g.index / 2, i => i.number)
.ToList();

//It will iterate through each groups
foreach (var group in numberGroup)
{
Console.WriteLine($"{group.Key} , count: {group.Count()}");
//Iterate through each number of a group
foreach (var number in group)
Console.WriteLine($" number : {number}");
}
}
}
}

output:

1
2
3
4
5
6
7
8
0 ,  count: 2
number : 00001
number : 00002
1 , count: 2
number : 00003
number : 00004
2 , count: 1
number : 00005