简体   繁体   中英

What does the dataRowFilter do?

What does the dataRowFilter do? is it only a condition for the rows? because adding a select in the row filter does not work

The code is:

Dim strExpr = "ClientID>(select count(*) from Client)-10" (to select the last 10 records);

Dim dv = ds.Tables(0).DefaultView;

dv.RowFilter = strExpr;
Dim newDS = New DataSet();
Dim newDT = dv.ToTable();
newDS.Tables.Add(newDT);

But the code does not work , if I put in strExpr="ClientID>3" the code does work so is the RowFilter only a condition? because I cant put select in it.

The DataRowFilter property is used to filter which rows are viewed in the DataView . The string is internally parsed into DataExpressions . You'll find more information about the expression syntax over at MSDN when looking at the Expression property of the DataColumn class. The same syntax applies to the Select function of the DataTable .

In these LINQ times it's quite trivial to query a data-set/table. For instance, if you have a Client table with indices ranging from 1 to 100 , then the following code will return clients with indices 31 to 40 .

Dim table As DataTable = (
    From row As DataRow
    In ds.Tables("Client")
    Let clientId As Int32 = row.Field(Of Int32)("ClientID")
    Where clientId > 30
    Order By clientId Ascending
    Select row
).Take(10).CopyToDataTable()

PS: The same query can actually be written as a one-liner:

Dim table As DataTable = (From row As DataRow In ds.Tables("Client") Let clientId As Int32 = row.Field(Of Int32)("ClientID") Where clientId > 30 Order By clientId Ascending Select row).Take(10).CopyToDataTable()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM