Find longest string in every DataTable column

Hi Experts
List<int> maxLenCols =
                  Enumerable.Range(0, tblData.Columns.Count)
                  .Select(col => tblData.AsEnumerable()
                  .Select(row => row[col]).OfType<string>()
                  .Max(val => val.Length)).ToList();

Open in new window

The above statement I got from here and it really worked perfectly for the purpose. The only downside that it works assuming all columns of string type, however my DataTable may contain int, DateTime, double.

The question is, how can we modify the statement to accept more data types ?

Many thanks.
Faraj1969System AdministratorAsked:
Who is Participating?
 
Daniel Van Der WerkenIndependent ConsultantCommented:
Just throwing a suggestion out there. I'm sorry but I haven't tested this myself. Would "casting" the column value to a string and just using that suffice?

List<int> maxLenCols =
                  Enumerable.Range(0, tblData.Columns.Count)
                  .Select(col => tblData.AsEnumerable()
                  .Select(row => row[col]).ToString()
                  .Max(val => val.Length)).ToList();

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi  Faraj1969;

Try this, it should give you what you are looking for.

List<int> maxLenCols = dt.AsEnumerable().
                       Select( row => 
                               (from cell in row.ItemArray 
                                select cell.ToString().Length
                               ).Max(x => x)
                             ).ToList();

Open in new window

0
 
Faraj1969System AdministratorAuthor Commented:
Dan7el : I got the following error before compiling :

Error      1      'char' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference?)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Fernando SotoRetiredCommented:
Hi  Faraj1969;

Have you tried my solution yet?
0
 
Faraj1969System AdministratorAuthor Commented:
Fernando: The method works somehow, but the behavior is a bit strange, The tblData.Columns.Count = 7, so I expect to have 7 values in the maxLenCols list, however with your method I got 1000.

The point of statement is to get the length of the longest string (or anything that can be casted as string) in each column.
0
 
Fernando SotoRetiredCommented:
So are you say that you want the longest value for each column so that you have 7 values returned?
0
 
Faraj1969System AdministratorAuthor Commented:
Yes Fernando, exactly the point. The statement I copied in the question works perfectly if all columns in tblData are of string type. But what if the type is different for one or two columns ?
0
 
Faraj1969System AdministratorAuthor Commented:
Dan7el said cast column, and it kept ringing in my head, so I solved it this way:
maxLenCols =
            Enumerable.Range(0, tblData.Columns.Count)
            .Select(col => tblData.AsEnumerable()
            .Select(row => (row[col]).ToString()).OfType<string>()
            .Max(val => val.Length)).ToList();

Open in new window

0
 
Faraj1969System AdministratorAuthor Commented:
Many thanks Dan7el and Fernando for participating.

I'll accept Dan7el answer as it gave me the solution.
0
 
Fernando SotoRetiredCommented:
You can make one small change to your solution by changing this line
.Select(row => (row[col]).ToString()).OfType<string>()

Open in new window

To this
.Select(row => (row[col]).ToString())

Open in new window

Seeming it is already a string and no need to have to test to see if it is a string.
0
 
Faraj1969System AdministratorAuthor Commented:
Dan7el solved it logically, I just modified the syntax to work, so his answer is Best.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.