learner123
learner123

Reputation: 243

Is it possible to rewrite the below code in Linq/Lambda approach

So far I have done

foreach (DataRow myRow in targetTable.Rows)
 {
  htmlBuilder.Append("<tr align='left' valign='top'>");

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

  htmlBuilder.Append("</tr>");
 }

Any better way of doing so...like Linq/Lambda approach

Thanks

Upvotes: 2

Views: 156

Answers (3)

Jim Wooley
Jim Wooley

Reputation: 10418

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.

Upvotes: 1

Tom Brothers
Tom Brothers

Reputation: 6017

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));

Upvotes: 1

Phil Gan
Phil Gan

Reputation: 2863

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);

Upvotes: 1

Related Questions