DataTable.Select 抛出 EvaluateException: 无法对 System.Double 和 System.String 执行'='操作



我正在将数据从多个电子表格导入到单个数据库表中。

首先,我将整个 excel 文件加载到数据集中,1 页 = 1 个表。 然后,我遍历了所有页面,并将列添加到新的组合表中。 最后,我将获取每一行,在其他页面中找到相应的行,并将数据复制到新表中的新行中。 我需要使用 3 列的组合来执行此匹配:"品牌"、"型号"和"年份"。 这是最后一步的循环。

//Add Data
foreach (DataRow drBase in tableSet.Tables[0].Rows)
{               
    List<DataRow> drSelect = new List<DataRow>(); //selected rows for specific bike
    foreach(DataTable dt in tableSet.Tables)
    {
        string expression="";
        foreach (string colName in joiningCols)
        {
            if (drBase[colName].ToString() == "") break;
            if (!string.IsNullOrWhiteSpace(expression))
            {
                expression += " and ";
            }
            expression += string.Format("{0} = '{1}'",colName, drBase[colName].ToString().Trim());
        }
        DataRow[] temp= { };
        if (!string.IsNullOrWhiteSpace(expression))
        {
        temp = dt.Select(expression);   //This is the line throwing the exception                     
        }
        if (temp.Length == 1)
        {
            drSelect.Add(temp[0]);
            //Debug.Print(string.Format("Match found {0} on {1}", expression, dt.TableName));
        }
        else
        {
            Debug.Print(string.Format("Incorrect number of matches ({2}) to <{1}> on Table[{0}]", dt.TableName, expression, temp.Length));
            continue;
        }
    }
    if (drSelect.Count == 2)
    {
        DataRow current = resultTable.NewRow();
        for (int t = 0; t < tableSet.Tables.Count; t++)
        {
            foreach (DataColumn c in tableSet.Tables[t].Columns)
            current[c.ColumnName] = drSelect[t][c.ColumnName];
        }
        resultTable.Rows.Add(current);
    }                
}

例外情况是:

EvaluateException was unhandled
An unhandled exception of type 'System.Data.EvaluateException' occurred in System.Data.dll
Additional information: Cannot perform '=' operation on System.Double and System.String.

例外期间的"表达式"值为"品牌 = '宝马',年份 = '1997-2000',车型 = 'F 650'"

错误和我的研究表明,我应该将所有值括为字符串,我已经做到了。 excel 中的所有列都没有使用特殊格式,因此所有列都应默认为文本。 唯一可能只包含数字的列是年份,但由于它能够在停止之前进行多次迭代,我不相信错误指向另一行。

经过一些测试,我将表达式分解为多个部分(A、B 和 C),它仅在选择 A 和 C 时崩溃("品牌 = '宝马'和 Yr = '1997-2000'")而不是当我单独选择每个子句时。

我错过了什么? 它试图比较的这个替身在哪里?

尝试

"Yr >= 1997 and Yr =< 2000"

你仍在打一场"猜数据类型"的战斗。

我仍然建议转换为ICollection<MyHolderObject>

"Excel File"

将所有内容视为字符串。 我将结果放入一个简单的类中,具有可设置的"字符串"属性。然后具有具有正确数据类型的相应"get"属性。然后我可以针对它运行查询/过滤器。

public class ExcelImportRow
{
    public string SalaryString { get; set; }
    /* if zero is ok */
    public double Salary
    {
        get
        {
            double returnValue = 0.00D;
            if (!string.IsNullOrEmpty(this.SalaryString))
            {
                double.TryParse(this.SalaryString, out returnValue);
            }
            return returnValue;
        }
    }
    public string TaxRateString { get; set; }
    /* if zero is not ok */
    public decimal? TaxRate
    {
        get
        {
            decimal? returnValue = null;
            if (!string.IsNullOrEmpty(this.TaxRateString))
            {
                decimal tryParseResult;
                if (decimal.TryParse(this.TaxRateString, out tryParseResult))
                {
                    returnValue = tryParseResult;
                }
            }
            return returnValue;
        }
    }
}

Excel 不是数据库。在Excel中,任何工作表单元格都可以包含任何类型的数据。

我不熟悉 Excel Data Reader,但它可能使用与 Excel ODBC/OLE 驱动程序相同的模糊逻辑,即:有一个 RowsToScan 参数,告诉驱动程序它应该扫描多少非标题行来猜测每列的数据类型,默认值为 1 行。

参考: Excel ODBC 驱动程序可能确定错误的数据类型

您的 Excel 文件可能发生的情况是,前几行在 Yr 列中包含数字数据,因此 Excel 数据阅读器推断 Yr 数据类型为十进制(它/其他东西正在设置该数据类型在数据表的 Yr 列上)。当您到达包含"1997-2000"的行时,该值无法转换为小数,因此您的例外:DataTable[Yr] 列的类型是十进制,您的比较值是字符串类型。

相关内容

最新更新