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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.