Convert ExpandoObject to DataTable

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; }

Convert DataTable to Dictionary

/// <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 →

Entity Framework 6 – Get edmx connection string

/// <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 →