Select Max value from DataTable

  • Thread starter Thread starter John Xavier Smith
  • Start date Start date
J

John Xavier Smith

Is there a more optimal way than creating a new column as an expression? The
following code snippet works, I was hoping for a better alternative:

<DataTable>.Columns.Add("MaxIndentation", typeof(string),
"Max(indentation)");
MessageBox.Show(<DataTable>.Rows[0]["MaxIndentation"].ToString());
 
-----Original Message-----
Is there a more optimal way than creating a new column as an expression? The
following code snippet works, I was hoping for a better alternative:

<DataTable>.Columns.Add("MaxIndentation", typeof(string),
"Max(indentation)");
MessageBox.Show(<DataTable>.Rows[0] ["MaxIndentation"].ToString());

How about...

<DataTable>.DefaultView.RowFilter = "indentation DESC";
int minIndentation = <DataTable>.DefaultView[0]
["indentation"];

This should work for MAX and MIN, but of course not for
things like COUNT and AVG.

-- TB
 
AFAIK, if you don't want to use an expression or query the database again,
you'll need to walk the column....
 
Neither way seems 'optimal'. Speed is not important in this run-once data
migration app anyway, I was just curious.

Thanks for the help and Happy New Year!
 
Back
Top