Sometimes you need a simpler version of an object with only a few properties. You can manually ignore properties but this can be long winded on bigger classes. In AutoMapper 5 you can pass in the destination type in the CreateMap consructor…Continue Reading →
Snippet /// <summary> /// Create a datatable from a list of ExpandoObjects /// </summary> /// <param name=”list”>The list can be created from a dictionary with Dictionary.Values.ToList()</param> /// <param name=”tableName”>Name of the data table</param> /// <returns></returns> public static DataTable ToDataTable(this List<ExpandoObject> list, string tableName) { if (list == null || list.Count == 0) return null; //build columns var props = (IDictionary<string, object>)list[0]; var t = new DataTable(tableName); foreach (var prop in props) { t.Columns.Add(new DataColumn(prop.Key, prop.Value.GetType())); } //add rows foreach (var row in list) { var data = t.NewRow(); foreach (var prop in (IDictionary<string, object>)row) { data[prop.Key] = prop.Value; } t.Rows.Add(data); } return t; }
/// <summary> /// Converts a DataTable to a dictionary. (NON PERFORMANT) /// </summary> /// <remarks> /// This method is usually used when running dynamic sql in databases that don’t support /// stored procs. The columns returned are unknown so we…Continue Reading →
/// <summary> /// Extensions for Entity Framework DbContext /// </summary> public static class DbContextExtensions { /// <summary> /// Gets the edmx connection string from an active DbContext. /// </summary> /// <param name=”context”>The context.</param> /// <returns></returns> public static string GetEdmxConnectionString(this DbContext…Continue Reading →
MVC’s separation is fantastic but some times you have areas that need to use a common controller. This is a question I see often asked online. If you have a common model that is used in different areas like a…Continue Reading →
var res = String.Join(“,”, array.Where(s => !string.IsNullOrEmpty(s)));