使用where子句过滤数据表行 [英] Filtering a datatable row using a where clause

查看:128
本文介绍了使用where子句过滤数据表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


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

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

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

I searched google, but could not find a solution.

我的代码是:

mySqlDataAdapter.Fill(myDataSet);

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

我该如何解决?

推荐答案

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

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子句过滤数据表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆