Question

This table only exists in memory, so can't do a quick SQL query.

I need to find the longest string in a column of a datatable.

Due to the size of the tables processed I can not just do a raw row-by-row size check, but have to use RowFilter.

I've tried these:

 dv.RowFilter = "[" + colName + "] = MAX([" + colName + "])"//incorrect results

and

dv.RowFilter = "[" + colName + "] = MAX(LEN([" + colName + "]))" // Gives error "Expecting a single column argument with possible 'Child' qualifier."

and

dv.RowFilter = "MAX(LEN([" + colName + "]))" // Gives error "Expecting a single column argument with possible 'Child' qualifier."

Any suggestions?

UPDATE:

OK, So I got it working with the following:

dataView.RowFilter = "[fieldsize_" + colName + "] = MAX([fieldsize_" + colName + "])"; //add column for size
dataTableSize = dataView.ToTable();

string strMaxValue = dataTableSize.Rows[0][column.ColumnName].ToString();
int colSize =  strMaxValue.Length;
Was it helpful?

Solution

Can you try on these lines

string maxstring = (string)dt.Compute("MAX(C1)","ISNULL(C1,'') <> ''");

here C1 is your concerned columnname and you get the max string value in the column and you could easily find the length from it.

This works for non-text columns.

For text columns, you can go about adding an additional column which would be evaluated as LEN(C1) (using Expression property of the new columm and then you can filter based on MAX

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top