Does DataSet take index of rows of a table that matches name = value?

0

Given a DataSet with table name = table1 I have several rows. Of all the rows I want to select the index or indices of the one that contains the element with the name of element = element and value = valueA

With:

DataSet ds = new DataSet();
ds.Tables["tabla1"].Rows.count;

I get the total number of rows

With:

DataSet ds = new DataSet();
ds.Tables["tabla1"].Rows[2]["elemento"]; 

I get a value

I need to implement a method that gives value and its key="element" returns index 2 or indexes that match.

Is that how I do it so that I return the index of the element of the rows that is equal to valueA?

Is it possible to do it without for or foreach?

    
asked by Popularfan 31.08.2018 в 15:47
source

1 answer

1

You can search all the rows that contain that value with LINQ and then see the index of each row with the collection of rows

string claveColumna = "elemento";
string valorAbuscar = "asd";
DataTable dt = new DataTable();
DataRow[] drList = dt.Select($"{claveColumna} like %{valorAbuscar}%");
List<int> indices = new List<int>();
Array.ForEach(drList, dr => indices.Add(dt.Rows.IndexOf(dr)));
    
answered by 31.08.2018 / 19:53
source