Professional Documents
Culture Documents
Lecture-05
column
Add
a column that aggregates data from a table, or from its parent or child table
Build
Generate
Aggregating Data
An
aggregation function returns a single calculated value from a set of related values. ADO.NET includes seven aggregation functions for use in expression columns and other DataTable features. Calculates the total of a set of column values. The column being summed must be numeric, either integral or decimal. : Returns the average for a set of numbers in a column. This function also requires a numeric column.
Sum:
Avg
Cont
Min: Indicates the minimum value found within a set of column values. Numbers, strings, dates, and other types of data that can be placed in order are all valid for the target column.
Max: Like Min, but returns the largest value from the available column values. As with the Min function, most column types will work.
Count: Simply counts the number of rows included in the aggregation. You can pass any type of column to this function. As long as a row includes a non-NULL value in that column, it will be counted as 1. StDev: Determines the statistical standard deviation for a set of values, a common measure of variability within such a set. The indicated column must be numeric. Var: Calculates the statistical variance for a set of numbers, another measurement re-lated to the standard deviation. Only numeric columns are supported.
Dim employees As New DataTable("Employee") employees.Columns.Add("ID", GetType(Integer)) employees.Columns.Add("Gender", GetType(string)) employees.Columns.Add("FullName", GetType(String)) employees.Columns.Add("Salary", GetType(Decimal)) ' ----- Add employee data to table, then... Dim averageSalary As Decimal = CDec(employees.Compute("Avg(Salary)", ""))
The Compute method calculates the average of the values in the Salary column. The second argument to Compute is a flter that limits the rows included in the calculation.
' ----- Build the child table and add some data. The "Total" ' expression column adds sales tax to the subtotal.
' ----- Two sample orders for customer 1, 1 for customer 2. orders.Rows.Add({1, 1, 35.24, 0.0875}) ' Total = $38.32 orders.Rows.Add({2, 1, 56.21, 0.0875}) ' Total = $61.13 orders.Rows.Add({3, 2, 14.94, 0.0925}) ' Total = $16.32
Cont
' ----- Link the tables within a DataSet. Dim business As New DataSet business.Tables.Add(customers)
business.Tables.Add(orders)
business.Relations.Add(customers.Columns!ID, orders.Columns!CustomerID)
' ----- Display each customer's order total. For Each scanCustomer As DataRow In customers.Rows
TextBox1.Text &= (CStr(scanCustomer!Name) & ": " & scanCustomer!OrderTotals & vbCrLf)
Next scanCustomer
DataTable.Select method lets you apply a selection query to a table, returning a subset of the available rows in the DataTable.
Its
convenient, but if you will run the same query against the table repeatedly, its not the most efficient use of computing resources.
DataView class
ADO.NET
includes the DataView class. As with the DataTable class, each DataView exposes a set of DataRow objects. But unlike the DataTable, the DataView does not actually contain any DataRow instances. Instead, it contains an index that refers to rows in a true DataTable. builds this index using the same query expressions used by the DataTable.Select method, with support for both a row selection component and a sorting component.
It
Creating a DataView
To
create a DataView from a DataTable, pass the table to the DataView constructor.
new view includes all the rows in the original table, sorted in the order they appear in the DataTable. alter the included rows, set the DataView objects RowFilter property.
Using a DataView
The
DataView class includes several features that return information about the in-view rows. The most basic is the DataView.Count property
Dim managersOnly As New DataView(employees) managersOnly.RowFilter = "IsManager = True" MessageBox.Show("Total Managers: " & managersOnly.Count)
Questions???