c# - How to convert to System.Int32 when doing a select from a DataTable -
i've got datatable i've loaded csv file using kbcsv library. can tell columns numbers in csv file , there aren't null rows. want select on data table query i've created using string.format. here's query:
string qry = string.format("gridx = {0} , gridy = {1} , convert([{2}], 'system.int32') {3} {4}", xc, yc, _testdatacolumn, _comparison, _threshold);
where xc double, yc double, _testdatacolumn string, _comparison string , _threshold double. table.select() method converts string to:
gridx = 0 , gridy = 4 , convert([st devssi(dbm)], 'system.int32') = 5
i put convert in there because before getting a
cannot perform '=' operation on system.string , system.int32
error message. i've looked @ this help. don't understand how last column (st devssi) became string. doing conversion correctly? there way take care of such errors?
if run code sample csv file should same error when searches coordinates (0, 4) looks problem near end of csv file, i'm still not sure.
a full example of code:
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} , gridy = {1} , [{2}] {3} {4}", xc, yc, _testdatacolumn, _comparison, _threshold); var results = table.select(qry); if (results.length > 0) { console.writeline(string.format("found match {0}, {1}", xc, yc)); } else { console.writeline(string.format("found match {0}, {1}", xc, yc)); } } } _streamreader.close(); } } }
that sample code gives me error above attempted use convert in query statement, can't seem work.
there's lot going on here, fix specific problem you're reporting:
- when building query string, make sure include decimal point threshold parameter value. simplest way
{4:f}
in format string.
the reason works because csv library you're using makes of values in data table have string types. when filter, each value being converted string type of the value being compared against. if compare "3"
3
, string converted integer , works. comparing "3.5"
3
won't return false, fails because "3.5"
can converted integer. comparing against value decimal point convert string double instead of integer, , works.
it's going slow heck though. it's parsing strings crazy here, , data table loaded memory. there better approaches - loading csv real database has indexes.
Comments
Post a Comment