gpt4 book ai didi

c# - 这个分割序列的linq代码是如何工作的?

转载 作者:太空狗 更新时间:2023-10-29 22:12:33 24 4
gpt4 key购买 nike

我一直在盯着我在另一个问题(下面逐字复制)中找到的这段代码,我知道它并不多,但它让我无法理解 i++ % 部分是如何工作的。

void Main()
{
int[] a = new [] { 10, 3, 5, 22, 223 };

a.Split(3).Dump();

}
static class LinqExtensions
{
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
int i = 0;
var splits = from item in list
group item by i++ % parts into part
select part.AsEnumerable();
return splits;
}
}

enter image description here

最佳答案

它将项目分组为您指定数量的部分。 i++ 将为每个项目递增并应用模数运算符,以便将其放入正确数量的桶中。所以第一个项目进入桶 0,第二个进入桶 1,第三个进入桶 2,第四个进入桶 0,等等......

关于c# - 这个分割序列的linq代码是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10238624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com