C#:划分一个字符串数组到N个实例N个项目长干净的方法 [英] C#: Cleanest way to divide a string array into N instances N items long

查看:131
本文介绍了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.

我的E-mail地址的字符串数组。假定字符串数组是任意长度的 - 它可能有几个项目,也可能有一个伟大的很多项目。我想建立包括比方说,从字符串数组50的电子邮件地址另一个字符串,直到数组的结尾,每个50后调用发送操作,使用50个地址字符串中的send()方法。

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());

为什么会搞成与所有的可怕循环code左右,当你不就得了?您可以通过五要组的事情,然后将它们组大约五十个人。
这是该集团运营商是干什么用的!

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;

让我们假设有9个地址,索引零通过八个

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.

范围变量首页发生在每个值依次

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

我们然后再在每个相应的地址[指数] ,并将其分配到组。

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

什么组我们把它分配给?要组指数/ 3 。整数算术舍朝向在C#为零,因此索引为0,1和2成为0时由3.索引3,4中,当通过3.索引6,7除以5成为1分,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.

所以我们分配地址[0] 地址[1] 地址[ 2] 来组0,地址[3] 地址[4] 地址[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.

这是否有道理?

还记得的结果的查询前pression 的是查询其重新presents此操作的。它没有的执行的直到的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天全站免登陆