How to select min and max values of a column in a datatable?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Getting the minimum and maximum value from a DataTable column is straightforward when the column is clean and strongly typed. Real tables are usually messier: some rows contain DBNull, some imports store numbers as strings, and some reports need filtering first. A good solution should be short for the simple case and still behave predictably when the data is imperfect.
Use LINQ when the column type is already numeric
For most application code, LINQ is the clearest approach. Convert the DataTable into an enumerable sequence of rows, project the column, and call Min and Max.
This is easy to read, works well with filters, and keeps the column access strongly typed. If you already use LINQ elsewhere in the same query, it is usually the best choice.
Filter out DBNull and empty results
Production data often contains missing values. Calling Min or Max on an empty sequence throws an exception, so it is worth handling that case explicitly.
Materializing into a list once is useful here because it avoids walking the same projection twice. That matters when the selection logic is more complex than a single column access.
DataTable.Compute is compact for simple reports
If you want a short expression-style answer, DataTable.Compute can calculate aggregates directly and accepts a filter expression. It is old, but still handy for report-like code.
This approach is concise, but the expression syntax is string-based, so it becomes harder to maintain as conditions grow. For business logic with several filters or type conversions, LINQ is usually easier to refactor and test.
Handle columns stored as text
Some imported tables keep numbers in string columns. In that case, parse the values explicitly instead of relying on loose conversions.
Explicit parsing makes bad input visible and avoids surprises from automatic conversion rules. If invalid values are significant, log or collect them instead of silently dropping them.
A reusable helper keeps behavior consistent
If several parts of your application need this logic, hide it behind a helper so every caller handles missing columns and empty results the same way.
That small wrapper gives you one place to define whether missing data should throw, return null, or be filtered away.
Common Pitfalls
The most common mistake is assuming every row contains a valid number. DBNull values and malformed text are normal in imported data, so guard those cases before aggregating.
Another issue is enumerating the same sequence repeatedly. If the projection is expensive or includes parsing, materialize once and compute both values from the list.
Developers also forget to validate the column name. A renamed or misspelled column then fails only at runtime.
Finally, DataTable.Compute becomes hard to maintain when the filter logic grows. It is convenient for simple expressions, but LINQ is usually the better long-term choice.
Summary
- LINQ with
AsEnumerableis the clearest approach for numeric columns. - Filter
DBNullvalues and handle empty sequences explicitly. - '
DataTable.Computeis compact for simple aggregate expressions and filters.' - Parse string-based numbers yourself instead of relying on implicit conversion.
- Put repeated min and max logic in a helper so every caller gets the same behavior.

