C#-在Foreach循环中重用变量
我要做的是在C#-在Foreach循环中重用变量,c#,foreach,C#,Foreach,我要做的是在列表中查找连续数字组(Excel行号),并针对每一个连续数字块,整体删除行,而不是一次删除一行,因为有时我将遍历多达9K行并不断增长。我遇到的问题是我调整了foreach循环的方式,我需要重用最后一个被检查的非连续变量 示例:列表是行{23,22,21,17,16,15},我需要将23-21,然后是17-15拉出并删除块(这就是为什么它们按降序排列,自下而上)。循环进入!=如果17上的语句和起作用,但是17已经被使用,16是循环的下一个迭代,因此17永远不会被捕获为下一个连续编号分组
列表中查找连续数字组(Excel行号)
,并针对每一个连续数字块,整体删除行,而不是一次删除一行,因为有时我将遍历多达9K行并不断增长。我遇到的问题是我调整了foreach
循环的方式,我需要重用最后一个被检查的非连续变量
示例:列表是行{23,22,21,17,16,15},我需要将23-21,然后是17-15拉出并删除块(这就是为什么它们按降序排列,自下而上)。循环进入!=如果17上的
语句和起作用,但是17已经被使用,16是循环的下一个迭代,因此17永远不会被捕获为下一个连续编号分组的开始
我的问题是:有没有办法以这种方式保持17岁,以及一个新的连续组别的任何其他开始,还是我找错了方向
代码:
public void FindMatchingBlocks(字符串stateId,字符串[]rangeNames)
{
Excel.Worksheet wksht=wkbk.Sheets[“Sheet1”];
Excel.Range rng=wksht.Range[“$A$15:$A$23”];
字符串val;
字符串val2;
ListrowNums=新列表();
字符串rngStart=rangeNames[0].ToString();//给我“$A$15”
字符串rngEnd=rangeNames[1].ToString();//给我$A$23$
字符串[]tempArray=rngEnd.Split(“$”);
string end=tempArray[2].ToString();
List rowsToDelete=新列表();
foreach(Excel.Range,单位为rng)
{
if(range.Row
希望这是清楚的,我花了一点时间来弄清楚如何简洁地解释我需要什么。谢谢。我们有什么?一个整数序列 我们想要什么?整数范围的序列 首先在类型系统中表示它。对于整数序列,我们有
IEnumerable
。让我们做一个小类型:(这里使用C#6符号)
简单。我们方法的签名是什么?我们需要整数的输入和输出,所以:
static class MyExtensions
{
public static IEnumerable<MyRange> DescendingChunks(this IEnumerable<int> items)
我们从来没有放弃序列中的最后一件事
yield return new MyRange(high, low);
}
有道理吗?现在代替你的循环
foreach (int x in rowsToDelete)
我们有
foreach(MyRange range in rowsToDelete.DescendingChunks())
现在您知道要修改的范围了
超级奖金问题:还有另一个我没有列举的案例,因此在这个方法中有一个小错误。这是什么?花了一些时间,但我想出了一种简洁的方法,可以获取一个数字列表,找到连续的数字并将它们分组到一个列表中。希望如果有人发现这一点及其有用:
private void groupConsecutiveNumbers()
{
/* this could easily be changed to look for ascending numbered groups by switching some of the "-1" to "+1"
* and swapping the firstNum/endNum variables. */
int[] numArray = new int[]{ 50, 23, 22, 21, 15, 16, 14, 9, 5, 4, 3, 1};
int firstNum = 0;
int endNum = 0;
string grouping;
for (int i = 0; i < numArray.Length; i++)
{
//If there is only 1 member of the list, that will be the first and last member of the group
if (numArray.Length == 1)
{
firstNum = numArray[0];
endNum = numArray[0];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
}
//if the number is the first one in the list then it automatically is the first one in the first list
else if (i == 0)
{
firstNum = numArray[0];
}
/* if its not the first one in the list and it is equal to the previous list item minus one
* (contiguously descending), then enter this loop */
else if (numArray[i] == (numArray[i-1] - 1))
{
//if this is the last member of the list, it automatically is the last item in the range
if ((i + 1) == numArray.Length)
{
endNum = numArray[i];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
}
//if this isn't the last member of the list, exit the loop and continue with the next item.
else
{
continue;
}
}
/* if the item if its not the first one in the list and does NOT equal the last item minus one
* (not contiguously descending) then the previous item was the last contiguously descending
* item and the current item is the first item in the next group */
else if (numArray[i] != (numArray[i-1]-1))
{
endNum = numArray[i - 1];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
firstNum = numArray[i];
endNum = 0;
}
/* After all that testing,if the item is the last item in the list AND the first number in the group
* is also the last item in the list then the current item in the list is both the first and last member
* in the current group. */
if ((i + 1) == numArray.Length && firstNum == numArray[i])
{
endNum = numArray[i];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
}
}
}
private void groupcontinuevenumbers()
{
/*通过将一些“-1”切换到“+1”,可以很容易地将其更改为查找升序编号的组
*并交换firstNum/endNum变量。*/
int[]numaray=新int[]{50,23,22,21,15,16,14,9,5,4,3,1};
int firstNum=0;
int-endNum=0;
字符串分组;
for(int i=0;iforeach (int x in rowsToDelete)
foreach(MyRange range in rowsToDelete.DescendingChunks())
private void groupConsecutiveNumbers()
{
/* this could easily be changed to look for ascending numbered groups by switching some of the "-1" to "+1"
* and swapping the firstNum/endNum variables. */
int[] numArray = new int[]{ 50, 23, 22, 21, 15, 16, 14, 9, 5, 4, 3, 1};
int firstNum = 0;
int endNum = 0;
string grouping;
for (int i = 0; i < numArray.Length; i++)
{
//If there is only 1 member of the list, that will be the first and last member of the group
if (numArray.Length == 1)
{
firstNum = numArray[0];
endNum = numArray[0];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
}
//if the number is the first one in the list then it automatically is the first one in the first list
else if (i == 0)
{
firstNum = numArray[0];
}
/* if its not the first one in the list and it is equal to the previous list item minus one
* (contiguously descending), then enter this loop */
else if (numArray[i] == (numArray[i-1] - 1))
{
//if this is the last member of the list, it automatically is the last item in the range
if ((i + 1) == numArray.Length)
{
endNum = numArray[i];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
}
//if this isn't the last member of the list, exit the loop and continue with the next item.
else
{
continue;
}
}
/* if the item if its not the first one in the list and does NOT equal the last item minus one
* (not contiguously descending) then the previous item was the last contiguously descending
* item and the current item is the first item in the next group */
else if (numArray[i] != (numArray[i-1]-1))
{
endNum = numArray[i - 1];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
firstNum = numArray[i];
endNum = 0;
}
/* After all that testing,if the item is the last item in the list AND the first number in the group
* is also the last item in the list then the current item in the list is both the first and last member
* in the current group. */
if ((i + 1) == numArray.Length && firstNum == numArray[i])
{
endNum = numArray[i];
grouping = firstNum.ToString() + "-" + endNum.ToString();
lstGroups.Items.Add(grouping);
}
}
}