I should be able to do something like:
var table = new HtmlTable {
Rows = { // <-- error is here
from XmlNode p in node.SelectNodes("./property")
select new HtmlTableRow{
Cells = {
new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
}
}
}
};
But I can't, because the LINQ code isn't "expanded" so the compiler complains that while it expects an HtmlTableRow element between the braces assigned to Rows, you're giving it a collection. But, you can't assign the collection to Rows directly because it's a read-only property.
Bad class design (Rows being read-only... same as Cells fwiw) or bad language design in that there's no way to "expand" the results of the LINQ query to do what seems natural in that spot?
Instead, I have to do:
var rows =
from XmlNode p in node.SelectNodes("./property")
select new HtmlTableRow{
Cells = {
new HtmlTableCell{ InnerText = Server.HtmlEncode(p.Attributes["description"].Value) },
new HtmlTableCell{ InnerText = Server.HtmlEncode(p.InnerText) }
}
};
var table = new HtmlTable();
foreach (var r in rows)
table.Rows.Add(r);
which is redundant.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):