Skip to content

Instantly share code, notes, and snippets.

@kbrammer
Last active December 15, 2015 02:59
Show Gist options
  • Save kbrammer/5191341 to your computer and use it in GitHub Desktop.
Save kbrammer/5191341 to your computer and use it in GitHub Desktop.
Extension method to automatically create a generic HTML table from an IEnumerable<object>
public static object TableBuilder(IEnumerable<object> obj)
{
XElement result = new XElement("table");
//add header row using property names
result.Add( new XElement("tr",
obj.FirstOrDefault().GetType().GetProperties().Select (property => new XElement("th",property.Name))));
//add object values to table rows
foreach(object o in obj)
{
result.Add( new XElement("tr",
o.GetType().GetProperties().Select (property => new XElement("td",property.GetValue(o)))));
}
return result;
}
//LINQPAD Extension Method to update properties of an IEnumerable<object>
//Example: MyObj.Update(x => x.href = (x.href.StartsWith("/")) ? "http://www.url.com"+x.href : x.href);
public static void Update<TSource>(this IEnumerable<TSource> outer, Action<TSource> updater)
{
foreach (var item in outer)
{
updater(item);
}
}
//LINQPAD Extension Method to preview XElements as Raw HTML
public static void ToRawHTML(this object obj)
{
LINQPad.Util.RawHtml((XElement)obj).Dump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment