问题描述
我有一个从数据集中提取的数据表.在 DataTable 中,我想使用 Where 子句返回特定行.我查看了如何使用两个变量值从数据表中选择一行?"但出现错误p><块引用>
"不能将类型'System.Data.DataRow[]'隐式转换为'System.Data.DataRow'"
我搜索了谷歌,但找不到解决方案.
我的代码是:
mySqlDataAdapter.Fill(myDataSet);数据表 dtTable = myDataSet.Tables[0];DataRow dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString());
我该如何解决这个问题?
选择 即使您的查询只选择一行,DataTable 的方法也会返回一个 DataRow 数组
DataRow[] dr = dtTable.Select("VendorID = " + Session["VendorId"].ToString());
然后,如果您真的只希望一行,您可以轻松获取预期的行来检查数组的长度.在这种情况下,我认为实际上不需要花哨的 Enumerable 扩展方法
if(dr.Length > 0){字符串 avalue = dr[0]["AColumnName"].ToString();...}
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
"Cannot implicitly convert type 'System.Data.DataRow[]' to '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?
The Select method of a DataTable returns an array of DataRow even if your query selects only one row
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
if(dr.Length > 0)
{
string avalue = dr[0]["AColumnName"].ToString();
...
}
这篇关于使用 where 子句过滤数据表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!