Quickly or concisely determine the longest string per column in a row-based data collection

Posted by ccornet on Stack Overflow See other posts from Stack Overflow or by ccornet
Published on 2010-05-04T13:42:20Z Indexed on 2010/05/04 14:08 UTC
Read the original article Hit count: 219

Filed under:
|
|
|
|

Judging from the failure of my last inquiry, I need to calculate and preset the widths of a set of columns in a table that is being made into an Excel file. Unfortunately, the string data is stored in a row-based format, but the widths must be calculated in a column-based format. The data for the spreadsheets are generated from the following two collections:

var dictFiles = l.Items.Cast<SPListItem>().GroupBy(foo => foo.GetSafeSPValue("Category")).ToDictionary(bar => bar.Key);
StringDictionary dictCols = GetColumnsForItem(l.Title);

Where l is an SPList whose title determines which columns are used. Each SPListItem corresponds to a row of data, which are sorted into separate worksheets based on Category (hence the dictionary). The second line is just a simple StringDictionary that has the column name (A, B, C, etc.) as a key and the corresponding SPListItme field display name as the corresponding value. So for each Category, I enumerate through dictFiles[somekey] to get all the rows in that sheet, and get the particular cell data using SPListItem.Fields[dictCols[colName]].

What I am asking is, is there a quick or concise method, for any one dictFiles[somekey], to retrieve a readout of the longest string in each column provided by dictCols? If it is impossible to get both quickness and conciseness, I can settle for either (since I always have the O(n*m) route of just enumerating the collection and updating an array whenever strCurrent.Length > strLongest.Length). For example, if the goal table was the following...

Item#  Field1     Field2     Field3
1      Oarfish    Atmosphere Pretty
2      Raven      Radiation  Adorable
3      Sunflower  Flowers    Cute

I'd like a function which could cleanly take the collection of items 1, 2, and 3 and output in the correct order...

Sunflower, Atmosphere, Adorable

Using .NET 3.5 and C# 3.0.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ