C# 从某个列获取数据表中的行索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13952660/
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-10 10:12:46  来源:igfitidea点击:

Get row index in datatable from a certain column

c#asp.netsqldatabase

提问by Johan

| 1 | 2 | 3 |
+------------+
| A | B | C |
| D | E | F | 
| G | H | I |


System.Data.DataTable dt = new DataTable();

dt.Columns.Add("1");
dt.Columns.Add("2");
dt.Columns.Add("3");
dt.Rows.Add(new object[] { "A", "B", "C" });
dt.Rows.Add(new object[] { "D", "E", "F" });
dt.Rows.Add(new object[] { "G", "H", "I" });

int? index = null;

var rows = new System.Data.DataView(dt).ToTable(false, new[] {"1"}).Rows;

for (var i = 0; i < rows.Count; i++)
{
    if (rows[i].ItemArray.FirstOrDefault() as string == "A")
        index = i;
}

Is there any way to simplify this code for fetching the index of a certain row, with a column provided? In this case, index will be 0, since I'm iterating through the first column until i find "A". Feels like there should be a linq solution to this, but I can't figure it out.

有没有办法简化此代码以获取特定行的索引,并提供一列?在这种情况下,索引将为0,因为我正在遍历第一列,直到找到“A”。感觉应该有一个 linq 解决方案,但我无法弄清楚。

采纳答案by StuartLC

If you use the DataTableExtensions.AsEnumerable()method, you will be able to query your DataTable with LINQ. You can then use List<T>.FindIndexto determine the index of a given predicate:

如果您使用DataTableExtensions.AsEnumerable()方法,您将能够使用 LINQ 查询您的 DataTable。然后,您可以使用List<T>.FindIndex来确定给定谓词的索引:

int? index = new System.Data.DataView(dt).ToTable(false, new[] { "1" })
                .AsEnumerable()
                .Select(row => row.Field<string>("1")) // ie. project the col(s) needed
                .ToList()
                .FindIndex(col => col == "G"); // returns 2

回答by Nabheet

You could try to an identity column. However, I do not know your application so please consider the pros and the cons of adding an identity column to your datatable. Here is a reference to get you started - how to add identity column

您可以尝试使用标识列。但是,我不知道您的应用程序,因此请考虑向数据表添加标识列的利弊。这是让您入门的参考 -如何添加标识列

Hope this helps.

希望这可以帮助。

回答by Pete

var index = from row in dt.AsEnumerable()
            let r = row.Field<string>("1")
            where r == "A"
            select dt.Rows.IndexOf(row);

回答by Jay Sampat

Use linq operation, but for that your target framework should be 4.5. Import system.Data.DataExtensions and apply the linq query will help you out.

使用 linq 操作,但为此您的目标框架应该是 4.5。导入 system.Data.DataExtensions 并应用 linq 查询将帮助您。

回答by Yannick Blondeau

You should be able to use the DataTable.Selectmethod like this:

您应该能够使用这样的DataTable.Select方法:

DataRow[] foundRows;
string filter = "1 == A";
foundRows = dt.Select(filter);

foreach (DataRow dr in foundRows)
{
    Console.WriteLine("Index is " + dr.Table.Rows.IndexOf(dr));
}