C# 从数据表中选择时如何转换为System.Int32

C# 从数据表中选择时如何转换为System.Int32,c#,.net,C#,.net,我有一个数据表,我使用。据我所知,这些列都是csv文件中的数字,没有任何空行。我想使用使用String.Format创建的查询对数据表进行选择。问题是: String qry = String.Format("GRIDX = {0} AND GRIDY = {1} AND CONVERT([{2}], 'System.Int32') {3} {4}", xc, yc, _testDataColumn, _comparison, _threshold); 其中xc是双精度,yc是双精度,_tes

我有一个数据表,我使用。据我所知,这些列都是csv文件中的数字,没有任何空行。我想使用使用String.Format创建的查询对数据表进行选择。问题是:

String qry = String.Format("GRIDX = {0} AND GRIDY = {1} AND CONVERT([{2}], 'System.Int32') {3} {4}", xc, yc, _testDataColumn, _comparison, _threshold);
其中xc是双精度,yc是双精度,_testDataColumn是字符串,_comparison是字符串,_threshold是双精度。table.Select方法将字符串转换为:

GRIDX = 0 AND GRIDY = 4 AND CONVERT([ST DEVSSI(dBm)], 'System.Int32') = 5
我把那个皈依者放在那里是因为在我拿到

无法对System.String和System.Int32执行“=”操作

错误消息。我一直在寻求帮助。我不明白最后一列ST DEVSSI是怎么变成字符串的。我的转换正确吗?有没有其他方法来处理此类错误

如果使用示例运行代码,当它搜索坐标0、4时,应该会得到相同的错误,因此问题似乎接近CSV文件的结尾,但我仍然不确定

我的代码的完整示例:

using Kent.Boogaart.KBCsv;
using Kent.Boogaart.KBCsv.Extensions;
using Kent.Boogaart.KBCsv.Extensions.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestCSVReader
{
public partial class MainWindow : Window
{
    private String _csvFilePath;
    private String _tempFilePath;
    private StreamReader _streamReader;
    private HeaderRecord _headerRecord = new HeaderRecord();
    private Double _threshold = 5;
    private String _testDataColumn = "ST DEVSSI(dBm)";
    private String _comparison = "=";
    private String[] coordinates = { "0,1", "0,2", "0,4", "1,1", "1,2", "1,3", "1,4"};

    public MainWindow()
    {
        InitializeComponent();
        _tempFilePath = System.IO.Path.GetTempPath() + @"\temp.csv";
        _csvFilePath = "CSV FILE PATH";

        using (var streamReader = new StreamReader(_csvFilePath))
        {
            HeaderRecord headerRecord = new HeaderRecord(streamReader.ReadLine().Split(','));

            DataTable table = new DataTable();

            using (var reader = new CsvReader(streamReader))
            {
                reader.HeaderRecord = headerRecord;
                table.Fill(reader);
            }

            foreach (String coordinate in coordinates)
            {
                var xy = coordinate.Split(',');
                double xc = Double.Parse(xy[0]);
                double yc = Double.Parse(xy[1]);

                String qry = String.Format("GRIDX = {0} AND GRIDY = {1} AND [{2}] {3} {4}", xc, yc, _testDataColumn, _comparison, _threshold);
                var results = table.Select(qry);

                if (results.Length > 0)
                {
                    Console.WriteLine(String.Format("Found a match for {0}, {1}", xc, yc));
                }
                else
                {
                    Console.WriteLine(String.Format("Found a match for {0}, {1}", xc, yc));
                }
            }
        }

        _streamReader.Close();
    }
}
}

该示例代码给出了上述错误,因此我尝试在查询语句中使用CONVERT,但似乎无法使其正常工作。

在C中,有几种方法可以执行字符串到数字类型的转换:

样本1:

    string _str ="12.25";
   // throws exception if fail, relatively slow in case of invalid input
    double _dbl = Convert.ToDouble(_str);
样本2:

string _str ="12.25";
// throws exception if fail, relatively slow in case of invalid input
double _dbl = Double.Parse(_str);
样本3:

string _str ="12.25";
double _dbl;
// true if conversion OK, false is fail;
// very fast compare to other two: (1) and (2)
Double.TryParse(_str,out _dbl);

希望这会有所帮助。

这里有很多问题,但要解决您报告的具体问题:

构建查询字符串时,请确保包含阈值参数值的小数点。最简单的方法是格式字符串中的{4:f}。 之所以这样做,是因为您使用的CSV库只是使数据表中的所有值都具有字符串类型。过滤时,每个值都将从字符串转换为要与之进行比较的值的类型。因此,如果您将3与3进行比较,字符串将转换为整数,并且可以正常工作。但将3.5与3进行比较不仅会返回false,而且会失败,因为3.5可以转换为整数。与带小数点的值进行比较会将字符串转换为双精度而不是整数,然后一切正常


尽管如此,它还是会很慢。它在这里疯狂地解析字符串,数据表被加载到内存中。有很多更好的方法,比如将CSV加载到具有索引的真实数据库中。

如果你想考虑比数据更可亲的东西,请检查一下。如果你能创建一个,那会有帮助的。我添加了一个代码示例。我尝试过使用LINQ到CSV,但问题是程序可能需要处理许多不同的CSV文件,因此在运行前不知道列名的情况下,我无法为LINQ动态创建模型。您不必显示完整的CSV文件-只是一个小示例,类似于您描述的问题。好的,因此,我添加了一个最小的、完整的、可验证的示例:D.您是否能够得到相同的错误?另外,您可以调用reader.ReadHeaderRecord;而不是自己处理流-但这与您报告的问题无关。是的,这就是问题所在,谢谢您所做的一切。我不得不使用streamreader,因为原始CSV文件有一个非常规的头。在实际页眉之前有两行额外的行,每个页眉分为两行。