如何使值与表列上其范围内的行交互?

时间:2015-03-04 12:42:24

标签: c# variables datatable

我有一个像这样的数据表。

enter image description here

在某些时候,人们将被要求提供他们自己的测量,可以是-5 -4 -3 -2 -1等等......直到35岁。

我需要在结果测量值之间进行插值。

我可以做这样的事情

double usermeasurement;
//user inserts the measurement in a textbox

if(usermeasurement <5)
{ //interpolation between line 1 and line 2 of the table
}

else if (usermeasurement <15)
{ //interpolation between line 2 and 3 of the table..
} 
// and so on...

我的编程问题是,是否有更简单,更先进的方法使usermeasurement变量适合列measurement

我可以制作自己的插值方法,但是有一个更简单的数学运算吗?

1 个答案:

答案 0 :(得分:1)

如果您是从代码

中的DataTable中获取此内容
int userInput = 7;

DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("Measurement", typeof(int)));
dataTable.Columns.Add(new DataColumn("Value", typeof(string)));

dataTable.Rows.Add(-5, "-0,1");
dataTable.Rows.Add(5, "0,2");
dataTable.Rows.Add(15, "0,5");
dataTable.Rows.Add(25, "0,7");
dataTable.Rows.Add(35, "0,9");

int min = Convert.ToInt32(dataTable.Compute("MIN(Measurement)", "Measurement > " + userInput.ToString()));
int max = Convert.ToInt32(dataTable.Compute("MAX(Measurement)", "Measurement < " + userInput.ToString()));
DataRow[] rows = dataTable.Select("Measurement = " + min.ToString() + " OR " + "Measurement = " + max.ToString());

如果您要查询数据库,请使用以下查询

SELECT * FROM Measurements WHERE Measurement = (select MIN(Measurement) from Measurements WHERE Measurement > 16) OR Measurement = (select MAX(Measurement) from Measurements WHERE Measurement < 16)