过滤数据集 [英] Filtering DataSet
本文介绍了过滤数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataSet充分的costumers。我在想,如果有任何的方式来过滤数据集,只得到了我想要的信息。例如,要获得 CostumerName
和 CostumerAddress
为具有负荷消费 CostumerID = 1
I have a DataSet full of costumers. I was wondering if there is any way to filter the dataset and only get the information I want. For example, to get CostumerName
and CostumerAddress
for a costumer that has CostumerID = 1
这可能吗?
推荐答案
您可以使用 DataTable.Select
:
var strExpr = "CostumerID = 1 AND OrderCount > 2";
var strSort = "OrderCount DESC";
// Use the Select method to find all rows matching the filter.
foundRows = ds.Table[0].Select(strExpr, strSort);
或者你可以使用数据视图
:
ds.Tables[0].DefaultView.RowFilter = strExpr;
更新我不知道为什么你想有一个DataSet返回。不过,我会去用以下解决方案:
UPDATE I'm not sure why you want to have a DataSet returned. But I'd go with the following solution:
var dv = ds.Tables[0].DefaultView;
dv.RowFilter = strExpr;
var newDS = new DataSet();
var newDT = dv.ToTable();
newDS.Tables.Add(newDT);
这篇关于过滤数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文