score:1

Accepted answer

LINQ is generally used to select and operate on a sub-set of a collection, what you're doing doesn't really fit its purpose.

You could define your foreach loops as lambdas if you really wanted to, but you won't get any special benefit. ie:

Action<DataColumn> buildAction = (DataColumn targetColumn) =>
{
    htmlBuilder.Append("<td align='left' valign='top'>");
    htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
    htmlBuilder.Append("</td>");
};
targetTable.Columns.ForEach(buildAction);

score:1

I would not suggest using the follow because your code is more efficient. But since you are looking for an example to learn from… you could use the following LINQ statement to achieve the same results.

var sb = new StringBuilder();

const string ROWBEGIN = "<tr align='left' valign='top'>";
const string ROWEND = "</tr>";
const string CELLBEGIN = "<td align='left' valign='top'>";
const string CELLEND = "</td>";

targetTable.AsEnumerable()
           .Select(row => string.Format("{0}{1}{2}",
                                        ROWBEGIN,
                                        string.Join(string.Empty,
                                                    row.Table.Columns
                                                             .Cast<DataColumn>()
                                                             .Select(column => string.Format("{0}{1}{2}",
                                                                                             CELLBEGIN,
                                                                                             (row.IsNull(column) ? string.Empty : row[column].ToString()),
                                                                                             CELLEND))
                                                             .ToArray()
                                                    ),
                                        ROWEND)
           )
           .ToList()
           .ForEach(y => sb.Append(y));

score:1

Rather than thinking of this as a string, consider using LINQ to XML to generate the nodes:

var rows = from row in targetTable.Rows.AsEnumerable()
           select new XElement("tr", 
                new XAttribute("align", "left"), 
                new XAttribute("valign","top"),
                from column in targetTable.Columns.AsEnumerable()
                select new XElement("td", 
                    new XAttribute("align", "left"), 
                    new XAttribute("valign", "top"),
                    myRow[targetColumn.ColumnName].ToString()
                )
           );

Translating this into Lambda syntax:

var rows = targetTable.Rows.AsEnumerable().Select(row => new XElement("tr", 
                new XAttribute("align", "left"), 
                new XAttribute("valign","top"),
                targetTable.Columns.AsEnumerable().Select(column => new XElement("td", 
                    new XAttribute("align", "left"), 
                    new XAttribute("valign", "top"),
                    myRow[targetColumn.ColumnName].ToString()
                ))
           ));

If you need the result as a string. Just call ToString() on rows.

One of the main advantages of thinking of this as XML over using a string builder is that you will properly escape invalid strings like <>&" and ensure valid XHtml as a result.


Related Articles