Convert a C# model into a list of SqlParameters

Assumes properties are decorated with [Column("column_name")] attributes.

public static List<SqlParameter> ToSqlParameters<T>(this T obj)
{
  var result = new List<SqlParameter>();
  var properties = obj.GetType().GetProperties();
    
  properties.ToList().ForEach(propertyInfo => 
  {
    var column = propertyInfo.GetCustomAttribute<ColumnAttribute>();
    var notMapped = propertyInfo.GetCustomAttribute<NotMappedAttribute>();

    // see previous post for this method "IsNullOrDefault"
    if (column != null && notMapped = null && !propertyInfo.IsNullOrDefault(obj)) 
    {
      var fieldValue = propertyInfo.GetValue(obj, default);
      var sqlParam = new SqlParameter($"@{column.Name}", fieldValue);

      result.Add(sqlParam);
    }
  });

  return result;
}

Convert a DataTable into a List of field/value pairs

public static List<Dictionary<string, dynamic>> ToDict(this DataTable dt) 
{
    var tableDict = new List<Dictionary<string, dynamic>>();

    foreach (DataRow row in dt.Rows)
    {
        var rowDict = new Dictionary<string, dynamic>();

        foreach (DataColumn col in dt.Columns) 
        {
            var fieldValue = row[col].Equals(DBNull.Value)
                ? null
                : row[col];

            rowDict.Add(col.ColumnName, fieldValue);
        }

        tableDict.Add(rowDict);
    }

    return tableDict;
}