Select Max value from DataTable

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());
 
T

TB

-----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
 
W

William Ryan

AFAIK, if you don't want to use an expression or query the database again,
you'll need to walk the column....
 
J

John Xavier Smith

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top