无法在LINQ方法下施放类型的对象

发布于 2025-02-09 04:20:06 字数 1270 浏览 5 评论 0原文

我在下面的代码行号(var ColumnNames =)中获得以下错误。

`System.InvalidCastException: 'Unable to cast object of type 'System.Object' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'.'`

Javscript控制台中的代码

 public JsonResult SaveData(List<object> customers)
        {
            DataTable dt = new DataTable();
            
            var columnNames = ((Dictionary<string, object>)customers[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 customers)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i < columnNames.Count(); i++)
                {
                    dr[columnNames[i]] = customer[columnNames[i]];
                }
                dt.Rows.Add(dr);
            }

    

            return Json('1');

        }

显示传递数据

0 : {Id: "1", Name: "abc", Country: "uk", __rowNum__: 1}
1: {Id: "2", Name: "kala", Country: "us", __rowNum__: 2}
2: {Id: "3", Name: "Has", Country: "IN", __rowNum__: 3}

I am getting following error in below code line No (var columnNames = ).

`System.InvalidCastException: 'Unable to cast object of type 'System.Object' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'.'`

Code

 public JsonResult SaveData(List<object> customers)
        {
            DataTable dt = new DataTable();
            
            var columnNames = ((Dictionary<string, object>)customers[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 customers)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i < columnNames.Count(); i++)
                {
                    dr[columnNames[i]] = customer[columnNames[i]];
                }
                dt.Rows.Add(dr);
            }

    

            return Json('1');

        }

In javscript console shows passing data

0 : {Id: "1", Name: "abc", Country: "uk", __rowNum__: 1}
1: {Id: "2", Name: "kala", Country: "us", __rowNum__: 2}
2: {Id: "3", Name: "Has", Country: "IN", __rowNum__: 3}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2025-02-16 04:20:06

您需要进行反思。

var columnNames = customers[0]
    .GetType()
    .GetProperties()
    .Select(x => x.Name)
    .ToList();

foreach (var customer in customers)
{
    DataRow dr = dt.NewRow();
    for (int i = 0; i < columnNames.Count(); i++)
    {
        PropertyInfo prop = customer.GetType().GetProperty(columnNames[i]);
        dr[columnNames[i]] = prop.GetValue(customer);
    }

    dt.Rows.Add(dr);
}

sample .net小提琴

You need to work with the Reflection.

var columnNames = customers[0]
    .GetType()
    .GetProperties()
    .Select(x => x.Name)
    .ToList();

foreach (var customer in customers)
{
    DataRow dr = dt.NewRow();
    for (int i = 0; i < columnNames.Count(); i++)
    {
        PropertyInfo prop = customer.GetType().GetProperty(columnNames[i]);
        dr[columnNames[i]] = prop.GetValue(customer);
    }

    dt.Rows.Add(dr);
}

Sample .NET Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文