我有一个装满顾客的DataSet。我想知道是否有任何方法可以过滤数据集并只获得我想要的信息。例如,要获取具有CostumerID = 1的客户的CostumerName和CostumerAddress
有可能吗?
发布于 2011-05-15 18:43:14
您可以使用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); 或者,您可以使用DataView:
ds.Tables[0].DefaultView.RowFilter = strExpr; UPDATE我不知道为什么要返回一个DataSet。但我会采用以下解决方案:
var dv = ds.Tables[0].DefaultView;
dv.RowFilter = strExpr;
var newDS = new DataSet();
var newDT = dv.ToTable();
newDS.Tables.Add(newDT);发布于 2014-07-09 04:56:21
没有提到合并?
DataSet newdataset = new DataSet();
newdataset.Merge( olddataset.Tables[0].Select( filterstring, sortstring ));发布于 2014-02-21 06:14:21
以上两项都非常接近。这是我的解决方案:
Private Sub getDsClone(ByRef inClone As DataSet, ByVal matchStr As String, ByRef outClone As DataSet)
Dim i As Integer
outClone = inClone.Clone
Dim dv As DataView = inClone.Tables(0).DefaultView
dv.RowFilter = matchStr
Dim dt As New DataTable
dt = dv.ToTable
For i = 0 To dv.Count - 1
outClone.Tables(0).ImportRow(dv.Item(i).Row)
Next
End Subhttps://stackoverflow.com/questions/6007872
复制相似问题