Filtrage des données

J’ai un DataSet complet de clients. Je me demandais s’il existe un moyen de filtrer l’dataset et d’obtenir uniquement les informations que je veux. Par exemple, pour obtenir CostumerName et CostumerAddress pour un client qui a CostumerID = 1

C’est possible?

Vous pouvez utiliser 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); 

Ou vous pouvez utiliser DataView :

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

UPDATE Je ne suis pas sûr de savoir pourquoi vous voulez avoir un DataSet renvoyé. Mais je choisirais la solution suivante:

 var dv = ds.Tables[0].DefaultView; dv.RowFilter = strExpr; var newDS = new DataSet(); var newDT = dv.ToTable(); newDS.Tables.Add(newDT); 

Aucune mention de fusion?

 DataSet newdataset = new DataSet(); newdataset.Merge( olddataset.Tables[0].Select( filterssortingng, sortssortingng )); 

Les ci-dessus étaient vraiment proches. Voici ma solution:

 Private Sub getDsClone(ByRef inClone As DataSet, ByVal matchStr As Ssortingng, 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