C# 使用 foreach 进行迭代时如何知道行索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1540808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:38:07  来源:igfitidea点击:

How can I know a row index while iterating with foreach?

c#foreach

提问by Luiscencio

in the next example how can I know the current row index?

在下一个示例中,我如何知道当前行索引?

foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
}

采纳答案by Jon Skeet

I have a type in MiscUtilwhich can help with this - SmartEnumerable. It's a dumb name, but it works :) See the usage pagefor details, and if you're using C# 3 you can make it even simpler:

我在MiscUtil 中有一个类型可以帮助解决这个问题 - SmartEnumerable。这是一个愚蠢的名字,但它有效:) 有关详细信息,请参阅用法页面,如果您使用的是 C# 3,则可以使其更简单:

foreach (var item in temptable.Rows.AsSmartEnumerable())
{
    int index = item.Index;
    DataRow value = item.Value;
    bool isFirst = item.IsFirst;
    bool isLast = item.IsLast;
}

回答by Joseph

You have to create one yourself

你必须自己创造一个

var i = 0;
foreach (DataRow temprow in temptable.Rows)
{
    this.text = i;
    // etc
    i++;
}

or you can just do a for loop instead.

或者你可以只做一个 for 循环。

回答by Nick DeVore

int rowIndex = temptable.Rows.IndexOf(temprow);

回答by Erich

Either use a for-loop, or use an integer follow along:

要么使用 for 循环,要么使用整数跟随:

int count =0;
foreach (DataRow temprow in temptable.Rows)
{
    //count is the index of the row in the array temptable.Rows
    //this.text = temprow.INDEX????
    ++count;
}

回答by Filip Ekberg

You actually Don't. One of the beauties with foreach is that you don't have the extra set of code handling incrementing and checks on the length.

你实际上没有。foreach 的优点之一是您没有额外的一组代码处理增量和长度检查。

If you want to have your own Index you would have to do something like this

如果您想拥有自己的索引,则必须执行以下操作

int rowindex = 0;
foreach (DataRow temprow in temptable.Rows)
{
//this.text = temprow.INDEX????
    this.text = rowindex++;
}

回答by JaredPar

It's not possible with a standard foreach loop. The simplest way is to use a for loop

使用标准的 foreach 循环是不可能的。最简单的方法是使用for循环

for ( int i = 0; i < temptable.Rows.Count; i++ ) {
  DataRow temprow = (DataRow)temptable.Rows[i];
  ...
}

Another option is to use an extension method

另一种选择是使用扩展方法

public static void ForEachIndex<T>(this IEnumerable<T> e, Action<T,int> del) {
  var i = 0; 
  foreach ( var cur in e ) {
    del(cur,i);
  }
}

...

...

temptable.Rows.Cast<DataRow>.ForEachIndex((cur,index) 
{
  ...
});

回答by bendewey

You can use the standard forloop to get the index

您可以使用标准for循环来获取索引

for(int i=0; i<temptable.Rows.Count; i++)
{
   var index = i;
   var row = temptable.Rows[i];
}

回答by Yoopergeek

While LFSR's answer is right, I'm pretty sure calling .IndexOf on just about any collection/list is going to enumerate the list until it finds a the matching row. For large DataTable's this could be slow.

虽然 LFSR 的答案是正确的,但我很确定在几乎任何集合/列表上调用 .IndexOf 都会枚举列表,直到找到匹配的行。对于大型 DataTable,这可能会很慢。

It might be better to for (i = 0; i < temptable.Rows.Count; i++) { ... } over the table. That way you have the index without imposing a find-the-index tax.

最好 for (i = 0; i < temptable.Rows.Count; i++) { ... } 在桌子上。这样您就可以拥有索引,而无需征收查找索引税。

回答by adrianbanks

If you can use Linq, you can do it this way:

如果你可以使用 Linq,你可以这样做:

foreach (var pair in temptable.Rows.Cast<DataRow>()
                                   .Select((r, i) => new {Row = r, Index = i}))
{
    int index = pair.Index;
    DataRow row = pair.Row;
}

回答by toddmo

Hey there's a much faster way I think. No iteration required! First, declare a static variable for the Friend RowID Field of the DataRow:

嘿,我认为有一种更快的方法。无需迭代!首先,为 DataRow 的 Friend RowID Field 声明一个静态变量:

Private Shared RowIDFieldInfo As System.Reflection.FieldInfo = GetType(DataRow).GetField("_rowID", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)

Then All you need to do to use it is:

然后你需要做的就是使用它:

RowIDFieldInfo.GetValue(MyDataRow) - 1

I have not tested this after resorting or filtering. In my case I haven't a need to do that, so this works.

在诉诸或过滤之后,我还没有对此进行测试。在我的情况下,我不需要这样做,所以这是有效的。