title: Generic class for convert list of object to IEnumerable<SqlDataRecord>
body: <p>I've this object:</p>
  
  <pre><code>public class ConsentData
  {
      public string ConsentName { get; set; }
      public bool ConsentValue { get; set; }
      public DateTime ConsentDate { get; set; }
  }
  </code></pre>
  
  <p>I should pass a List of ConsentData to a stored procedure in SQL Server via a table-value parameter.</p>
  
  <p>Looking for a way to convert the List of ConsentData in a List of SqlDataRecord i found this generic class on web:</p>
  
  <pre><code>public class SqlTableValueSupport&lt;T&gt; : List&lt;T&gt;, IEnumerable&lt;SqlDataRecord&gt; where T : class, new()
      {
          IEnumerator&lt;SqlDataRecord&gt; IEnumerable&lt;SqlDataRecord&gt;.GetEnumerator()
          {
              //Create Columns (SqlMetaData)
              List&lt;SqlMetaData&gt; records = new List&lt;SqlMetaData&gt;();
              var properties = typeof(T).GetProperties();
              foreach (var prop in properties)
              {
                  SqlDbType sdbtyp = GetSqlType(prop.PropertyType);
                  records.Add(new SqlMetaData(prop.Name, sdbtyp));
              }
  
              //Create records/rows (SqlDataRecord)
              SqlDataRecord ret = new SqlDataRecord(records.ToArray());
  
              foreach (T data in this)
              {
                  for (int i = 0; i &lt; properties.Length; i++)
                  {
                      ret.SetValue(i, properties[i].GetValue(data, null));
                  }
  
                  yield return ret;
              }
          }
  
          // Map C# Types to SqlDbType
          private SqlDbType GetSqlType(Type type)
          {
              SqlDbType val = SqlDbType.VarChar;
              if (type == typeof(Int64) || type == typeof(Nullable&lt;Int64&gt;))
              {
                  val = SqlDbType.BigInt;
              }
              else if (type == typeof(Byte[]))
              {
                  val = SqlDbType.Binary;
              }
              else if (type == typeof(Boolean) || type == typeof(Nullable&lt;Boolean&gt;))
              {
                  val = SqlDbType.Bit;
              }
              else if (type == typeof(DateTime) || type == typeof(Nullable&lt;DateTime&gt;))
              {
                  val = SqlDbType.DateTime;
              }
              else if (type == typeof(Decimal))
              {
                  val = SqlDbType.Decimal;
              }
              // Please refer to the following document to add other types
              // http://msdn.microsoft.com/en-us/library/ms131092.aspx
              return val;
          }
      }
  </code></pre>
  
  <p>I'd like to know how to use the class, how can i pass the List of ConsentData and retrive a List of SqlDataRecord?</p>