在DataTable.Select()中将字符串值与double进行比较 [英] Compare string values with double in DataTable.Select()
本文介绍了在DataTable.Select()中将字符串值与double进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据工资列是 string
列和用户给出字符串格式的输入值,如 userInput =1000
。但数据存储在工资列中如下图所示。如何将用户输入与存储在数据表中的数据进行比较?
My datatable salary column is string
column and user gives input value in string format, like userInput="1000"
. But data stored in salary column is as shown in below image. How do I compare user input with data stored in data table?
我当前的代码是
DataRows[] rows = dt.Select("Salary='"+userInput+"'");
推荐答案
以避免格式化问题,您应该更好地比较数值。 DataTable选择过滤器支持转换
function:
to avoid formatting issues you should better compare numeric values. DataTable select filter supports Convert
function:
var dt = new DataTable();
dt.Columns.Add("Salary", typeof(string));
// test values
dt.Rows.Add("0.10000");
dt.Rows.Add(".1");
dt.Rows.Add("-.1");
dt.Rows.Add("1.1");
decimal salary;
string userInput = "0.10";
if (decimal.TryParse(userInput, out salary))
{
DataRow[] matchedRows = dt.Select("Convert(Salary, 'System.Decimal') = " + salary.ToString());
foreach(var r in matchedRows)
Console.WriteLine(r["Salary"]);
}
输出是:
.1
0.10000
这篇关于在DataTable.Select()中将字符串值与double进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文