开发者

Filtering DataSet

开发者 https://www.devze.com 2023-03-06 05:25 出处:网络
I have a DataSet full of costumers. I was wondering if there is any way to filter the dataset and only get the informa开发者_如何学运维tion I want. For example, to get CostumerName and CostumerAddress

I have a DataSet full of costumers. I was wondering if there is any way to filter the dataset and only get the informa开发者_如何学运维tion I want. For example, to get CostumerName and CostumerAddress for a costumer that has CostumerID = 1

Is it possible?


You can use 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);  

Or you can use DataView:

ds.Tables[0].DefaultView.RowFilter = strExpr;  

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);


No mention of Merge?

DataSet newdataset = new DataSet();

newdataset.Merge( olddataset.Tables[0].Select( filterstring, sortstring ));


The above were really close. Here's my solution:

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 Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消