C# 使用 where 子句过滤数据表行

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

Filtering a datatable row using a where clause

c#datatabledatasetwhere-clausedatarow

提问by Csharp

I have a DataTable that I am pulling from a DataSet. From the DataTable, I want to use the Where clause to return a particular row. I looked at "How can I select a row from a datatable using two variable values?" but I get an error

我有一个从数据集中提取的数据表。从数据表中,我想使用 Where 子句返回特定行。我查看了“如何使用两个变量值从数据表中选择一行?”但出现错误

"Cannot implicitly convert type 'System.Data.DataRow[]' to 'System.Data.DataRow'"

“无法将类型 'System.Data.DataRow[]' 隐式转换为 'System.Data.DataRow'”

I searched google, but could not find a solution.

我搜索了谷歌,但找不到解决方案。

My code is:

我的代码是:

mySqlDataAdapter.Fill(myDataSet);

DataTable dtTable = myDataSet.Tables[0];
DataRow dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString());

How can I resolve this?

我该如何解决这个问题?

采纳答案by Steve

The Selectmethod of a DataTable returns an array of DataRow even if your query selects only one row

即使您的查询仅选择一行,DataTable的Select方法也会返回一个 DataRow 数组

DataRow[] dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString());

Then, if you really expects just one row, you could easily grab the expected row checking for the length of the array. In this case it is my opinion that no fancy Enumerable extension methods are really needed

然后,如果您真的只需要一行,您可以轻松获取预期的行,检查数组的长度。在这种情况下,我认为真的不需要花哨的 Enumerable 扩展方法

if(dr.Length > 0)
{
    string avalue = dr[0]["AColumnName"].ToString();
    ...
}

回答by Jonesopolis

Select() is returning an array of rows, and you're assigning it to a single row.

Select() 返回一个行数组,您将其分配给一行。

You can do :

你可以做 :

DataRow dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString()).First();

or

或者

var dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString());

which will give you an array of rows.

这会给你一个行数组。

回答by Oscar

Asuming there is only one unique result

假设只有一个唯一结果

DataTable dtTable = myDataSet.Tables[0];
DataRow[] drs = dtTable.Select("VendorID = " + Session["VendorId"].ToString());
if(drs.Length > 0)
    DataRow dr = drs[0];

回答by Jason P

DataTable.Select()returns an array of DataRow.

DataTable.Select()返回一个数组DataRow

DataRow[] dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString());

If you only want the first record,

如果你只想要第一条记录,

DataRow[] dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString()).FirstOrDefault();

回答by Forte L.

Use the method First():

使用方法First()

DataRow dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString()).First();

回答by Harkonnen

The Select() method returns an IEnumerable collection with one DataRow inside, in your case. You must extract the DataRow from that collection with FirstOrDefault() instead.

在您的情况下, Select() 方法返回一个 IEnumerable 集合,其中包含一个 DataRow 。您必须改为使用 FirstOrDefault() 从该集合中提取 DataRow。