无法将类型为'Newtonsoft.Json.Linq.JObject'的对象强制转换为类型为'System.Collections.Generic.Dictionary`2 [System.String,System.Object]'

时间:2019-11-21 06:59:17

标签: c# angularjs asp.net-web-api

我正在尝试使用SqlBulkCopy 将对象列表插入表中。当代码行

 var columnNames = ((Dictionary<string, object>)bData[0]).Select(x => x.Key).ToList();'

我知道了

  

'无法将类型为'Newtonsoft.Json.Linq.JObject'的对象转换为   类型   'System.Collections.Generic.Dictionary`2 [System.String,System.Object]''   错误。

你能帮我吗?

public Int64 PostBulkMessage(List<object> bData)
{
    DataTable dt = new DataTable();

    var columnNames = ((Dictionary<string, object>)bData[0]).Select(x => x.Key).ToList();

    for (int i = 0; i < columnNames.Count(); i++)
    {
        dt.Columns.Add(columnNames[i]);
    }
    foreach (Dictionary<string, object> customer in bData)
    {
        DataRow dr = dt.NewRow();
        for (int i = 0; i < columnNames.Count(); i++)
        {
            dr[columnNames[i]] = customer[columnNames[i]];
        }
        dt.Rows.Add(dr);
    }
    if (dt.Rows.Count > 0)
    {
        string consString = ConfigurationManager.ConnectionStrings["ConnEntities"].ConnectionString;
        using (SqlConnection con = new SqlConnection(consString))
        {
            using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            {
                //Set the database table name.
                sqlBulkCopy.DestinationTableName = "dbo.BulkSent";
                con.Open();
                sqlBulkCopy.WriteToServer(dt);
                con.Close();
            }
        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

非常感谢Stefan和Rahul Sharma的支持。根据Stefan的建议,我创建了类并更改了代码。现在,批量插入工作正常。请验证以下代码。

public Int64 PostBulkMessage(List<Bulk> bData)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("Mobile", typeof(string));
            dt.Columns.Add("MessageContent", typeof(string));

            for (int i = 0; i < bData.Count(); i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = bData[i].Id;
                dr[1] = bData[i].Mobile;
                dr[2] = bData[i].MessageContent;

                dt.Rows.Add(dr);
            }

            if (dt.Rows.Count > 0)
            {
                string connectionString = ConfigurationManager.ConnectionStrings["ConnEntities"].ConnectionString;
                if (connectionString.ToLower().StartsWith("metadata="))
                {
                    System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder efBuilder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(connectionString);
                    connectionString = efBuilder.ProviderConnectionString;
                }
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                    {
                        sqlBulkCopy.DestinationTableName = "dbo.BulkMsg";
                        con.Open();
                        sqlBulkCopy.WriteToServer(dt);
                        con.Close();
                    }
                }
            }
            return 0;
        }


 public class Bulk
        {

            public string Id { get; set; }
            public string Mobile { get; set; }
            public string MessageContent { get; set; }
        }