For the following datatable column, what is the fastest way to get the min and max values?
AccountLevel 0 1 2 3
Easiar approach on datatable could be:
int minLavel = Convert.ToInt32(dt.Compute("min([AccountLevel])", string.Empty));
int minAccountLevel = int.MaxValue; int maxAccountLevel = int.MinValue; foreach (DataRow dr in table.Rows) { int accountLevel = dr.Field<int>("AccountLevel"); minAccountLevel = Math.Min(minAccountLevel, accountLevel); maxAccountLevel = Math.Max(maxAccountLevel, accountLevel); }
Yes, this really is the fastest way. Using the Linq Min
and Max
extensions will always be slower because you have to iterate twice. You could potentially use Linq Aggregate
, but the syntax isn't going to be much prettier than this already is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With