• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1584
  • Last Modified:

Find longest string in every DataTable column

Hi Experts

In this question I was able to get the length of the longest text in every column, can we modify the method to get the string itself, not the length ? To make it a little more clearer, assuming the tblData have 6 columns, the maxLenCols List should have 6 strings representing the longest string in every column.
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

Many thanks.
0
Faraj1969
Asked:
Faraj1969
1 Solution
 
Fernando SotoRetiredCommented:
Hi Faraj1969;

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

var macString = 
  Enumerable.Range(0, dt.Columns.Count)
    .Select(col => dt.AsEnumerable()
    .Select(row => (row[col]).ToString()).OfType<string>()
    .Aggregate("", (output, next) => (next.Length > output.Length) ? next : output)).ToList();

Open in new window

0
 
Faraj1969System AdministratorAuthor Commented:
Great solution, direct to the point.

Sorry for the simplicity of my question, but when it comes to Linq, I'm a total newbie.

Many thanks mate.
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now