Reputation: 3226
So I have 2 datatables, The 1st datatable is a traditional datatable which gets its data from certain sql statements.
Datatable 1:
| user_point | | lat | | lng | | radius |
--------------------------------------------------------------
| userpoint0 | | 43.702943 | | -79.37478 | | 3.10685596 |
| userpoint1 | | 43.672655 | | -79.479837 | | 4.970969536 |
However, the certain columns are dynamically generated in Datatable 2 based on the number of userpoints in Datatable 1.
| 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 |
Now I will add a new column at the end of datatable 2 called the Closest_userpoint
which will store the value of the distance which is the closest userpoint for that row.
So now my question is how to I find the the smallest/min value for that row, which has columns that are dynamically generated(which means there can be any number of userpoints from none to 100). I am looking for a simplest and most efficient answer, Thank you
EDIT:
Here is my code for how the dynamic columns are created, However I am stuck on figuring out the logic to fill the closestUserpoint Column with min value for that row based on the distancefromuserpoints
EDIT 2:
BONUS QUESTION:
Finally I want to determine if that closest Point/Min value falls within the raidus for that userpoint which it is closest.
Upvotes: 1
Views: 717
Reputation: 458
it will be easier if you try finding the minimum values after populating the values
use System.Linq; for the Min() method
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";
}
}
}
Upvotes: 1