Current code is working fine. However column headers are missing. In current example NRO, NRO1, SNAME are column headers.
I understand that <th> should be added to existing method but have hard times figuring out how?
<tr>
<th>NRO</th>
<th>NRO1</th>
<th>SNAME</th>
</tr>
Here is List to HTML table method (found on this website):
public static string GetMyTable<T>(IEnumerable<T> list, params Func<T, object>[] fxns)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table>\n");
foreach (var item in list)
{
sb.Append("<tr>\n");
foreach (var fxn in fxns)
{
sb.Append("<td>");
sb.Append(fxn(item));
sb.Append("</td>");
}
sb.Append("</tr>\n");
}
sb.Append("</table>");
return sb.ToString();
}
I use it like this:
var HTML = GetMyTable(duplicates, x => x.NRO, x => x.NRO1, x => x.SNAME);
I have tried to do it like this:
public static string GetMyTable<T>(IEnumerable<T> list, params Func<T, object>[] fxns)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table>\n");
sb.Append("<tr>\n");
sb.Append("<th>NRO</th>\n");
sb.Append("<th>NRO1</th>\n");
sb.Append("<th>SNAME</th>\n");
sb.Append("</tr>\n");
foreach (var item in list)
{
sb.Append("<tr>\n");
foreach (var fxn in fxns)
{
sb.Append("<td>");
sb.Append(fxn(item));
sb.Append("</td>");
}
sb.Append("</tr>\n");
}
sb.Append("</table>");
return sb.ToString();
}
Is it possible to make a loop for headers so there will be no need to assign them for each table separately?