Last active
December 15, 2015 02:59
-
-
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>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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