How do I use RowFilter to to find the length of the longest string in a column?

StackOverflow https://stackoverflow.com/questions/7305606

  •  25-10-2019
  •  | 
  •  

سؤال

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;
هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top