简体   繁体   English

如何修复System.InvalidCastException:指定的强制转换无效

[英]How to fix System.InvalidCastException: Specified cast is not valid

I have two Data Tables and want to combine them to form one Data Table based on their individual ID's. 我有两个数据表,并希望根据它们各自的ID将它们组合成一个数据表。

The column columnName should get summed up if the ID is duplicated across the two initial Data Tables. 如果ID在两个初始数据表中重复,则应该对columnName列进行汇总。

When I run my code I get this error 当我运行代码时,出现此错误

How to fix System.InvalidCastException: Specified cast is not valid 如何修复System.InvalidCastException:指定的强制转换无效

DataTable SubData = dsResult.Tables[0];

var query = SubData.AsEnumerable().GroupBy(row => new
{
    ProductID = row.Field<Int64>("ProductID"),
    PrdCode = row.Field<string>("PrdCode")
}).Select(grp =>
{
    dynamic result = new ExpandoObject();
    var dict = result as IDictionary<string, object>;
    result.ProductID = grp.Key.ProductID;
    result.PrdCode = grp.Key.PrdCode;

    foreach (DataRow row in grp)
    {
       foreach (DataColumn column in SubData.Columns)
       {
          string columnName = column.ColumnName;

          if (columnName.Equals("ProductID") || columnName.Equals("PrdCode"))
          {
              continue;
          }

          if (!dict.Keys.Contains(columnName))
          {
              dict[columnName] = row[columnName];
          }
          else
          {
             if (row[columnName] is System.DBNull)
             {
                continue;
             }

             if (dict[columnName] is System.DBNull)
             {
                 dict[columnName] = row[columnName];
                 continue;
             }

             dict[columnName] = (Int64)dict[columnName] + (Int64)row[columnName];
          }
      }
  }

  return result;
});

foreach (var item in query)
{
    dtGraph.Rows.Add(item);
}

Verify the data in 'dict'. 验证“ dict”中的数据。 There must be a value which you can't convert to 'int' 必须有一个不能转换为'int'的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid System.InvalidCastException:&#39;指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar System.InvalidCastException:指定的强制转换无效(linq查询) - System.InvalidCastException: Specified cast is not valid (linq query) System.InvalidCastException:指定的强制转换无效。 -DynamoDB查询 - System.InvalidCastException: Specified cast is not valid. - DynamoDB Query System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL C#System.InvalidCastException:指定的转换无效 - C# System.InvalidCastException: Specified Cast is not valid System.InvalidCastException:指定的强制转换无效的C# - System.InvalidCastException: Specified cast is not valid C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM