Search code examples
c#datatabledatarowdatacolumn

How can I access what a previous variable was set to?


I have a foreach nested inside a foreach. I need to check whether d is less than all the other d's. How can I do this? If there is a way to see what a variable was previously set to, then I would be able to do this myself. If not, could you please devise a solution?

Here is my code:

foreach (DataRow newRow1 in dt.Rows)
            {
                string zipCode1 = newRow1[2].ToString();
                double latitude2 = Convert.ToDouble(newRow1[3]);
                double longitude2 = Convert.ToDouble(newRow1[4]);

                foreach (DataRow newRow2 in dt2.Rows)
                {
                    if (newRow2[2].ToString().Equals(zipCode1))
                    {
                        newRow1[5] = newRow2[1].ToString();
                        double latitude = Convert.ToDouble(newRow1[3]);
                        double longitude = Convert.ToDouble(newRow1[4]);
                        double d = Math.Sqrt(Math.Abs(latitude - latitude2) * Math.Abs(latitude - latitude2) + Math.Abs(longitude - longitude2) * Math.Abs(longitude - longitude2));
                        Console.WriteLine("Found match!");
                    }
                }
            }

Solution

  • This is equivalent to the good old algorithm of finding min by a linear search:

    foreach (DataRow newRow1 in dt.Rows)
            {
                string zipCode1 = newRow1[2].ToString();
                double latitude2 = Convert.ToDouble(newRow1[3]);
                double longitude2 = Convert.ToDouble(newRow1[4]);
    
                // Start minD at the max value
                double minD = double.MaxValue;
                foreach (DataRow newRow2 in dt2.Rows)
                {
                    if (newRow2[2].ToString().Equals(zipCode1))
                    {
                        newRow1[5] = newRow2[1].ToString();
                        double latitude = Convert.ToDouble(newRow1[3]);
                        double longitude = Convert.ToDouble(newRow1[4]);
                        double d = Math.Sqrt(Math.Abs(latitude - latitude2) * Math.Abs(latitude - latitude2) + Math.Abs(longitude - longitude2) * Math.Abs(longitude - longitude2));
                        minD = Math.Min(minD, d);
                        Console.WriteLine("Found match!");
                    }
                }
                Console.WriteLine("Min D: {0}", minD);
            }