根据列表从datatable中查找值

时间:2021-05-16 20:12:14

I have a list name str in which series name is stored (like "Series 2","Series4") etc. and a DataTable named dtSeriesData in which values of the series are stored.

我有一个列表名称str,其中存储了系列名称(如“Series 2”,“Series4”)等,还有一个名为dtSeriesData的DataTable,其中存储了系列的值。

I want to fetch values from dtSeriesData based on series name in str list.

我想根据str列表中的系列名称从dtSeriesData中获取值。

1 个解决方案

#1


0  

So you want to filter the table, so that you only get rows which these series? You can use LINQ:

所以你想过滤表,这样你才能获得这些系列的行?您可以使用LINQ:

var filteredRows = dtSeriesData.AsEnumerable()
    .Where(row => str.Contains(row.Field<string>("ColumnName")));

If you want a new DataTable with the filtered rows use CopyToDataTable.

如果您想要一个带有过滤行的新DataTable,请使用CopyToDataTable。

You could also use Enumerable.Join which is more efficient. I use query syntax when it comes to join:

您还可以使用更高效的Enumerable.Join。我在连接时使用查询语法:

var filteredRows = from row in dtSeriesData.AsEnumerable()
                   join name in str
                   on row.Field<string>("ColumnName") equals name
                   select row;

#1


0  

So you want to filter the table, so that you only get rows which these series? You can use LINQ:

所以你想过滤表,这样你才能获得这些系列的行?您可以使用LINQ:

var filteredRows = dtSeriesData.AsEnumerable()
    .Where(row => str.Contains(row.Field<string>("ColumnName")));

If you want a new DataTable with the filtered rows use CopyToDataTable.

如果您想要一个带有过滤行的新DataTable,请使用CopyToDataTable。

You could also use Enumerable.Join which is more efficient. I use query syntax when it comes to join:

您还可以使用更高效的Enumerable.Join。我在连接时使用查询语法:

var filteredRows = from row in dtSeriesData.AsEnumerable()
                   join name in str
                   on row.Field<string>("ColumnName") equals name
                   select row;

相关文章