C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We call the DataTable Select Function. This returns an array of DataRow instances.
DataRowQuery: We use the query (Size >= 230 AND Team = "b"). So only data rows with a Size greater than 229 and a Team of "b" are returned.
VB.NET program that uses DataTable, Select
Module Module1
Sub Main()
' Create a table.
Dim table As DataTable = New DataTable("Players")
' Add 2 columns.
table.Columns.Add(New DataColumn("Size", GetType(Integer)))
table.Columns.Add(New DataColumn("Team", GetType(Char)))
' Add 5 rows.
table.Rows.Add(100, "a"c)
table.Rows.Add(235, "a"c)
table.Rows.Add(250, "b"c)
table.Rows.Add(310, "b"c)
table.Rows.Add(150, "b"c)
' Get players above 230 size with "b" team.
Dim result() As DataRow = table.Select("Size >= 230 AND Team = 'b'")
' Loop and display.
For Each row As DataRow In result
Console.WriteLine("{0}, {1}", row(0), row(1))
Next
End Sub
End Module
Output
250, b
310, b
Rows: The rows contain the ID of the widget and the DateTime the widget was built. For DateTimes, we use the constructor.
DateTimeThen: We pass the query string containing a DateTime substring to the Select Function.
Note: For using a DateTime query, we can use the numeric comparison operators. We must surround the date with pound "#" symbols.
VB.NET program that uses Select, date
Module Module1
Sub Main()
' Create a table.
Dim table As DataTable = New DataTable("Widgets")
' Add 2 columns.
table.Columns.Add(New DataColumn("ID", GetType(Integer)))
table.Columns.Add(New DataColumn("Date", GetType(DateTime)))
' Add rows.
table.Rows.Add(100, New DateTime(2001, 1, 1))
table.Rows.Add(200, New DateTime(2002, 1, 1))
table.Rows.Add(300, New DateTime(2003, 1, 1))
' Get rows more recent than 6/1/2001.
Dim result() As DataRow = table.Select("Date > #6/1/2001#")
' Loop.
For Each row As DataRow In result
Console.WriteLine(row(0))
Next
End Sub
End Module
Output
200
300
VB.NET program that causes EvaluateException
Module Module1
Sub Main()
Dim table As DataTable = New DataTable("Players")
' We must use known columns, or an EvaluateException will occur.
Dim result() As DataRow = table.Select("X")
End Sub
End Module
Output
Unhandled Exception: System.Data.EvaluateException: Cannot find column [X].
at System.Data.NameNode.Bind(DataTable table, List`1 list)
at System.Data.DataExpression.Bind(DataTable table)
at System.Data.DataExpression..ctor(DataTable table, String expression, Type type)
at System.Data.DataTable.Select(String filterExpression)
Info: This operator is used in the same way as in an SQL query. Select() makes DataTables act like small databases.