devxlogo

The DataTable’s Compute method

The DataTable class has a method, Compute, that executes SQL-like functions on the rows locally stored in the DataTable. It supports functions such as COUNT, SUM, MIN, MAX, AVG and others. Here’s an example to calculate the average salary for the employees stored in the tableEmployees DataTable:

Dim maxSalary As Integer = CType(tableEmployees.Compute("AVG(Salary)", ""), _    Integer)

The Compute method takes in a second parameter, which is a filter expression. For example, this is how to calculate the number of employees with a salary greater than $5,000:

Dim numLuckyEmployees As Integer = CType(tableEmployees.Compute _    ("COUNT(EmployeeID)", "Salary > 5000"), Integer)

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.