private List <Node> GetNeighbourNodes(string name) { DbDataReader reader = null; try { // Some cities have multiple airports. We don't need this information here, // so we use distinct here and get the field we need to save space and time. reader = ApplicationDb.RunQuery($"Select DISTINCT Destination from routes.csv where Origin=\"{name}\""); var result = new List <Node>(); while (reader.Read()) { string neighbourName = reader["Destination"].ToString(); //If this node(airport) has already added, we don't want to create a duplicate. That breaks the algorithm. var neighbourNode = AllNodes.FirstOrDefault(n => n.Name == neighbourName); if (neighbourNode == null) { neighbourNode = new Node { Name = neighbourName, }; AllNodes.Add(neighbourNode); //We only need to process this neighbour if it hasn't already been processed. result.Add(neighbourNode); } } return(result); } finally { if (reader != null && !reader.IsClosed) { reader.Close(); } // We don't close the connection, because we are going to run a lot of queries against the database // and it is not efficient to close the connection after each query. } }