C#:将字符串数组划分为 N 个实例 N 项长的最简洁方法 [英] C#: Cleanest way to divide a string array into N instances N items long

查看:47
本文介绍了C#:将字符串数组划分为 N 个实例 N 项长的最简洁方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何以一种丑陋的方式做到这一点,但我想知道是否有更优雅、更简洁的方法.

I know how to do this in an ugly way, but am wondering if there is a more elegant and succinct method.

我有一个电子邮件地址的字符串数组.假设字符串数组的长度是任意的——它可能有几个项目,也可能有很多项目.我想构建另一个字符串,其中包含来自字符串数组的 50 个电子邮件地址,直到数组末尾,并在每 50 个之后调用一次发送操作,使用 Send() 方法中的 50 个地址的字符串.

I have a string array of e-mail addresses. Assume the string array is of arbitrary length -- it could have a few items or it could have a great many items. I want to build another string consisting of say, 50 email addresses from the string array, until the end of the array, and invoke a send operation after each 50, using the string of 50 addresses in the Send() method.

更普遍的问题是做这种事情的最干净/最清晰的方法是什么.我有一个解决方案,它是我学习 VBScript 的遗产,但我打赌在 C# 中有更好的方法.

The question more generally is what's the cleanest/clearest way to do this kind of thing. I have a solution that's a legacy of my VBScript learnings, but I'm betting there's a better way in C#.

推荐答案

你要优雅简洁,我给你优雅简洁:

You want elegant and succinct, I'll give you elegant and succinct:

var fifties = from index in Enumerable.Range(0, addresses.Length) 
              group addresses[index] by index/50;
foreach(var fifty in fifties)
    Send(string.Join(";", fifty.ToArray());

为什么在不需要的时候处理所有那些糟糕的循环代码?你想把东西按五十年代分组,然后按五十年代分组.这就是组运算符的用途!

Why mess around with all that awful looping code when you don't have to? You want to group things by fifties, then group them by fifties. That's what the group operator is for!

更新:评论者 MoreCoffee 询问这是如何工作的.假设我们想按三个一组进行分组,因为这样更容易输入.

UPDATE: commenter MoreCoffee asks how this works. Let's suppose we wanted to group by threes, because that's easier to type.

var threes = from index in Enumerable.Range(0, addresses.Length) 
              group addresses[index] by index/3;

假设有九个地址,索引从 0 到 8

Let's suppose that there are nine addresses, indexed zero through eight

这个查询是什么意思?

Enumerable.Range 是从零开始的九个数字的范围,所以 0, 1, 2, 3, 4, 5, 6, 7, 8.

The Enumerable.Range is a range of nine numbers starting at zero, so 0, 1, 2, 3, 4, 5, 6, 7, 8.

范围变量 index 依次采用这些值中的每一个.

Range variable index takes on each of these values in turn.

然后我们遍历每个对应的addresses[index]并将其分配给一个组.

We then go over each corresponding addresses[index] and assign it to a group.

我们将它分配给哪个组?对 index/3 进行分组.C#中整数算法向零舍入,因此索引0、1和2在除以3时变为0.索引3、4、5除以3时变为1.索引6、7、8变为2.

What group do we assign it to? To group index/3. Integer arithmetic rounds towards zero in C#, so indexes 0, 1 and 2 become 0 when divided by 3. Indexes 3, 4, 5 become 1 when divided by 3. Indexes 6, 7, 8 become 2.

因此我们将 addresses[0]addresses[1]addresses[2] 分配给组 0,addresses[3]addresses[4]addresses[5] 到组 1,依此类推.

So we assign addresses[0], addresses[1] and addresses[2] to group 0, addresses[3], addresses[4] and addresses[5] to group 1, and so on.

查询的结果是三组的序列,每组是三项的序列.

The result of the query is a sequence of three groups, and each group is a sequence of three items.

有意义吗?

还请记住,查询表达式的结果是一个查询,表示该操作.在 foreach 循环执行之前,它不会执行操作.

Remember also that the result of the query expression is a query which represents this operation. It does not perform the operation until the foreach loop executes.

这篇关于C#:将字符串数组划分为 N 个实例 N 项长的最简洁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆