所以我有2个数据表,第一个数据表是传统的数据表,它从某些sql语句中获取数据。
数据表1:
| user_point | | lat | | lng | | radius |
--------------------------------------------------------------
| userpoint0 | | 43.702943 | | -79.37478 | | 3.10685596 |
| userpoint1 | | 43.672655 | | -79.479837 | | 4.970969536 |
但是,某些列是根据Datatable 1中的用户点数在Datatable 2中动态生成的。
| id | | lat | | lng | | DistanceFromUserpoint0 | | DistanceFromUserpoint1 |
-------------------------------------------------------------------------------------------------------------
| 23184 | | 43.6495246887207 | | -79.4244003295898 | | 4.4464409231533 | | 3.19880848195014 |
| 37957 | | 43.6372413635254 | | -79.4151458740234 | | 4.96760996486758 | | 4.05524888969018 |
| 37965 | | 43.636589050293 | | -79.4169921875 | | 5.04670564187353 | | 4.00990129127938 |
| 60467 | | 43.735538482666 | | -79.4437942504883 | | 4.11692339031897 | | 4.70303114025665 |
| 60475 | | 43.735538482666 | | -79.4437942504883 | | 4.11692339031897 | | 4.70303114025665 |
| 65615 | | 43.7292861938477 | | -79.4317932128906 | | 3.37923630122185 | | 4.59015403452972 |
| 65623 | | 43.7292861938477 | | -79.4317932128906 | | 3.37923630122185 | | 4.59015403452972 |
| 3196486 | | 43.6624603271484 | | -79.4242172241211 | | 3.7316961595166 | | 2.86768157143755 |
| 3196494 | | 43.6624603271484 | | -79.4242172241211 | | 3.7316961595166 | | 2.86768157143755 |
| 5756393 | | 43.719165802002 | | -79.4295654296875 | | 2.95683309139676 | | 4.07847187106957 |
| 5756922 | | 43.719165802002 | | -79.4295654296875 | | 2.95683309139676 | | 4.07847187106957 |
| 5756956 | | 43.719165802002 | | -79.4295654296875 | | 2.95683309139676 | | 4.07847187106957 |
| 5756991 | | 43.719165802002 | | -79.4295654296875 | | 2.95683309139676 | | 4.07847187106957 |
| 5757096 | | 43.719165802002 | | -79.4295654296875 | | 2.95683309139676 | | 4.07847187106957 |
| 5757134 | | 43.719165802002 | | -79.4295654296875 | | 2.95683309139676 | | 4.07847187106957 |
现在我将在数据表2的末尾添加一个名为Closest_userpoint
的新列,它将存储该行最近用户点的距离值。
所以现在我的问题是如何找到该行的最小/最小值,其中包含动态生成的列(这意味着可以有从0到100的任意数量的用户点)。我正在寻找一个最简单,最有效的答案,谢谢
修改
这是关于如何创建动态列的代码。但是,我仍然坚持要根据距离用户点
确定用最小值来填充该行的最小值的逻辑。编辑2:
奖金问题:
最后,我想确定最接近的Point / Min值是否属于最接近的用户点的raidus。
答案 0 :(得分:1)
如果在填充值
后尝试查找最小值,将会更容易使用System.Linq;对于Min()方法
if (userpoints.Rows.Count > 1)
{
foreach (DataRow dr in selectedPanels.Rows)
{
List<string> alluserpoints = new List<string>();
for (int i = 0; i < userpoints.Rows.Count; i++)
{
if (Convert.ToDouble(userpoints.Rows[i].radius) > Convert.ToDouble(dr["DistanceFromUserpoint" + i]))
{
alluserpoints.Add(dr["DistanceFromUserpoint" + i]+"+userpoint"+i);
}
}
if(alluserpoints.Count>0)
dr["Closest_UserPoint"] = alluserpoints.Min();
else
dr["Closest_UserPoint"] ="none";
}
}
else
{
foreach(DataRow dr in selectedPanels.Rows)
{
if(Convert.ToDouble(userpoints.Rows[0].radius>Convert.ToDouble(dr["DistanceFromUserpoint0"])
{
dr["Closest_UserPoint"]=dr["DistanceFromUserpoint0"]+"+userpoint0";
}
else
{
dr["Closest_UserPoint"]="none";
}
}
}