将DataTable转换为动态类

时间:2015-10-30 12:09:10

标签: c#

我试图在C#中将数据表转换为智能表。基本上存储过程返回dataTable,我需要将其转换为JavaScript对象,但我不知道我将拥有哪些列名。智能表是角度框架,需要特定的格式,例如下面的例子:

$scope.columnCollection = ["firstName", "lastName", "birthDate", "balance", "email"];

$scope.rowCollection = [
    { firstName: 'Laurent', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'whatever@gmail.com' },
    { firstName: 'Blandine', lastName: 'Faivre', birthDate: new Date('1987-04-25'), balance: -2323.22, email: 'oufblandou@gmail.com' },
    { firstName: 'Francoise', lastName: 'Frere', birthDate: new Date('1955-08-27'), balance: 42343, email: 'raymondef@gmail.com' }
];

所以我创建了这个代码来翻译它:

     public class SmartTable
     {
          public IList<string> ColumnCollection { get; set; }
          public IList<Object> RowCollection { get; set; }
     }


        public SmartTable GetTableData(string sp, int widgetId = 0)
    {
        var connection = ConfigurationManager.ConnectionStrings["EFDbContext"].ConnectionString;
        using (var da = new SqlDataAdapter("exec " + sp, connection))
        {
            DataTable dt = new DataTable();

            da.Fill(dt);
            da.FillSchema(dt, SchemaType.Mapped);

            IList<string> colColls = new List<string>();

            foreach(DataColumn dc in dt.Columns)
            {
                colColls.Add(dc.Caption);
            }

            IList<Object> rowColls = new List<Object>();

            foreach (DataRow dr in dt.Rows)
            {
                dynamic rowColl = new Object();

                foreach (DataColumn dc in dt.Columns)
                {
                    rowColl.Add(dc.Caption, dr[dc].ToString());
                }
                rowColls.Add(rowColl);

            }

            SmartTable smartTable = new SmartTable
            {
                ColumnCollection = colColls,
                RowCollection = rowColls
            };

        return smartTable;
        };
    }

但是当我运行这个时,我会收到错误&#34;&#39; object&#39;不包含&#39;添加&#39;&#34;的定义并突出显示以下行:

rowColl.Add(dc.Caption, dr[dc].ToString());

知道如何解决这个问题吗?

修改

如果我一直期望静态数据,我会像下面这样做:

public class Row
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string birthDate { get; set; }
    ...
}

但问题是我不知道我会拥有什么和多少列。

2 个答案:

答案 0 :(得分:3)

您的问题在这里:

  foreach (DataRow dr in dt.Rows)
  {
       dynamic rowColl = new Object();

       foreach (DataColumn dc in dt.Columns)
       {
           rowColl.Add(dc.Caption, dr[dc].ToString());
       }
       rowColls.Add(rowColl);

   }

我不确定你为什么要使用动态 - 只需使用字典,就像这样:

   var dictionary = new Dictionary<string, List<object>>();
   foreach (DataColumn dataColumn in dataTable.Columns)
   {
       var columnValueList = new List<object>();

       foreach (DataRow dataRow in dataTable.Rows)
       {
            columnValueList.Add(dataRow[dataColumn.ColumnName]);
       }

       dictionary.Add(dataColumn.ColumnName, columnValueList);
   }

答案 1 :(得分:0)

@returnsvoid回答没有给出正确的数据结构,但它给了我如何做到这一点。如果有人对确切答案感兴趣:

foreach(DataColumn dc in dt.Columns)
            {
                colColl.Add(dc.Caption);
            }

            IList<Dictionary<string, string>> rowColl = new List<Dictionary<string, string>>();

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

                foreach (DataColumn dc in dt.Columns)
                {
                    row.Add(dc.ColumnName, dr[dc].ToString());
                }
                rowColl.Add(row);
            }

            SmartTable smartTable = new SmartTable
            {
                ColumnCollection = colColl,
                RowCollection = rowColl
            };