我如何从数据表中获取某些特定值? [英] How I can take some particular value from data table?
问题描述
我有ds中的数据,我想填写数据表
之后,我想从该表中只取特定的行,这样我怎么做呢?????? />
我的尝试:
i have data in ds and i want to fill in data table
after that i want to take only particular row from that table so how i can do that??????
What I have tried:
oBuyerBO.Ds.Tables.Add(dt);
foreach (DataRow row in dt.Rows)
{
if (row["CompanyID"].ToString() != hidSelectedSupplier.ClientID)
{
return row["CompanyName"];
}
}
推荐答案
至少 - 很少有办法实现这一目标...... >
1)使用Linq:
There's - at least - few ways to achieve that...
1) using Linq:
//returns the list of rows which meet some criteria
var resultRow = dt.AsEnumerable()
.Where(x => x.Field<int>("CompanyID") != hidSelectedSupplier.ClientID)
.ToList();</int>
2)使用Filter ans选择方法:
< a href =https://msdn.microsoft.com/en-us/library/zk13kdh0%28v=vs.90%29.aspx>如何:直接在数据表中筛选和排序 [ ^ ]
试试!
2) using Filter ans Select methods:
How to: Filter and Sort Directly in Data Tables[^]
Try!
您应该使用DataTable对象的Select()方法,如下所示。
Hi,
You should use Select() method of the DataTable object like shown below.
string filterCondition = string.format(CultureInfo.CurrentCulture, "{0} = '{1}'", "CompanyName", "Test"); // assuming that you have a column called CompanyName in the DataTable and you are searching for the rows where CompanyName = "Test"
DataRow[] selectedRows = dt.Select(filterCondition);
foreach(DataRow rowItem in selectedRows)
{
// Do your stuff
}
如果您正在寻找,请告诉我。
谢谢,
Debasis。
Please let me know if this is what you are looking for.
Thanks,
Debasis.
dt = oBuyerBO.Ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
if (oCompanyBO.SelectedSupplierIDs.ToString() != row["CompanyID"].ToString())
{
lstSelectedSuppliers.Value= row["CompanyName"].ToString();
}
}
i我正在尝试这个
但是此代码不检查if条件
i想要显示不在oCompanyBO.SelectedSupplierIDs中的值
...
i am trying this
but this code do not check the if condition
i want to display the value which is not in the oCompanyBO.SelectedSupplierIDs
...
这篇关于我如何从数据表中获取某些特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!