如何从DataTable获取特定列值?

时间:2021-08-03 15:51:01

I have a datatable. I need to fetch a certain column value based on the user input. For example, lets say the datatable has two columns CountryID and CountryName.

我有一个数据表。我需要根据用户输入获取某个列值。例如,假设数据表有两列CountryID和CountryName。

I need to find CountryID in the datatable based on the user input country name. I could just open a connection with DB and run the query select countryID from Country where countryName = @userinput. Is there anyway i could do this on the datatable.

我需要根据用户输入的国家/地区名称在数据表中找到CountryID。我可以打开与DB的连接,并从countryName = @userinput的Country运行查询select countryID。无论如何我可以在数据表上执行此操作。

5 个解决方案

#1


61  

string countryName = "USA";
DataTable dt = new DataTable();
int id = (from DataRow dr in dt.Rows
              where (string)dr["CountryName"] == countryName
              select (int)dr["id"]).FirstOrDefault();

#2


16  

foreach (DataRow row in Datatable.Rows) 
{
    if (row["CountryName"].ToString() == userInput) 
    {
        return row["CountryID"];
    }
}

While this may not compile directly you should get the idea, also I'm sure it would be vastly superior to do the query through SQL as a huge datatable will take a long time to run through all the rows.

虽然这可能无法直接编译,但您应该明白这一点,我也相信通过SQL进行查询将会非常优越,因为巨大的数据表需要很长时间才能运行所有行。

#3


8  

I suggest such way based on extension methods:

我建议这种方式基于扩展方法:

IEnumerable<Int32> countryIDs =
    dataTable
    .AsEnumerable()
    .Where(row => row.Field<String>("CountryName") == countryName)
    .Select(row => row.Field<Int32>("CountryID"));

System.Data.DataSetExtensions.dll needs to be referenced.

需要引用System.Data.DataSetExtensions.dll。

#4


1  

Datatables have a .Select method, which returns a rows array according to the criteria you specify. Something like this:

数据表有一个.Select方法,它根据您指定的条件返回行数组。像这样的东西:

Dim oRows() As DataRow

oRows = dtCountries.Select("CountryName = '" & userinput & "'")

If oRows.Count = 0 Then
   ' No rows found
Else
   ' At least one row found. Could be more than one
End If

Of course, if userinput contains ' character, it would raise an exception (like if you query the database). You should escape the ' characters (I use a function to do that).

当然,如果userinput包含'character,则会引发异常(就像查询数据库一样)。你应该逃避'字符(我使用一个函数来做)。

#5


0  

I suppose you could use a DataView object instead, this would then allow you to take advantage of the RowFilter property as explained here:

我想你可以使用DataView对象,这样你就可以利用RowFilter属性,如下所述:

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

private void MakeDataView() 
{
    DataView view = new DataView();

    view.Table = DataSet1.Tables["Countries"];
    view.RowFilter = "CountryName = 'France'";
    view.RowStateFilter = DataViewRowState.ModifiedCurrent;

    // Simple-bind to a TextBox control
    Text1.DataBindings.Add("Text", view, "CountryID");
}

#1


61  

string countryName = "USA";
DataTable dt = new DataTable();
int id = (from DataRow dr in dt.Rows
              where (string)dr["CountryName"] == countryName
              select (int)dr["id"]).FirstOrDefault();

#2


16  

foreach (DataRow row in Datatable.Rows) 
{
    if (row["CountryName"].ToString() == userInput) 
    {
        return row["CountryID"];
    }
}

While this may not compile directly you should get the idea, also I'm sure it would be vastly superior to do the query through SQL as a huge datatable will take a long time to run through all the rows.

虽然这可能无法直接编译,但您应该明白这一点,我也相信通过SQL进行查询将会非常优越,因为巨大的数据表需要很长时间才能运行所有行。

#3


8  

I suggest such way based on extension methods:

我建议这种方式基于扩展方法:

IEnumerable<Int32> countryIDs =
    dataTable
    .AsEnumerable()
    .Where(row => row.Field<String>("CountryName") == countryName)
    .Select(row => row.Field<Int32>("CountryID"));

System.Data.DataSetExtensions.dll needs to be referenced.

需要引用System.Data.DataSetExtensions.dll。

#4


1  

Datatables have a .Select method, which returns a rows array according to the criteria you specify. Something like this:

数据表有一个.Select方法,它根据您指定的条件返回行数组。像这样的东西:

Dim oRows() As DataRow

oRows = dtCountries.Select("CountryName = '" & userinput & "'")

If oRows.Count = 0 Then
   ' No rows found
Else
   ' At least one row found. Could be more than one
End If

Of course, if userinput contains ' character, it would raise an exception (like if you query the database). You should escape the ' characters (I use a function to do that).

当然,如果userinput包含'character,则会引发异常(就像查询数据库一样)。你应该逃避'字符(我使用一个函数来做)。

#5


0  

I suppose you could use a DataView object instead, this would then allow you to take advantage of the RowFilter property as explained here:

我想你可以使用DataView对象,这样你就可以利用RowFilter属性,如下所述:

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

private void MakeDataView() 
{
    DataView view = new DataView();

    view.Table = DataSet1.Tables["Countries"];
    view.RowFilter = "CountryName = 'France'";
    view.RowStateFilter = DataViewRowState.ModifiedCurrent;

    // Simple-bind to a TextBox control
    Text1.DataBindings.Add("Text", view, "CountryID");
}