locked
Build Dictionary from 2 tables RRS feed

  • Question

  • User-1270425977 posted

    Hi I have a link and a node table as follows. I would need to create a number of lines based on the nodeid and also the node type. Where type=vertex, I would only add a vertex to the line to represent change in direction.

     
    
    NodeID   Coords   Type
    A         x1,y1    Valve
    B         x2,y2    Valve
    C         x3,y3    Valve
    D         x4,y4    Vertex
    E         x5,y5    Valve
    
    Fromnode   Tonode
    A           B
    B           C
    C           D
    D           E
    

    Expected Output

    Line 1 coordinates: (x1,y1), (x2,y2)

    Line 2 coordinates: (x2,y2), (x3,y3)

    Line 3 coordinates: (x3,y3), (x4,y4), (x5,y5)

    With C# I would like to be able to loop through the 2 tables, and build a dictionary (or similar) with line number and coordinates, so that I can go and then create the geometry for the lines.

    Any ideas how can I go about achieving this?

    Thanks

    Monday, October 3, 2016 9:24 AM

Answers

  • User283571144 posted

    Hi sunsilk10,

    With C# I would like to be able to loop through the 2 tables, and build a dictionary (or similar) with line number and coordinates, so that I can go and then create the geometry for the lines.

    Any ideas how can I go about achieving this?

    According to your description, I suggest you could use list to store all the result and you need to add a new class to store each coordinates.

    More details, you could refer to follow codes:

    class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("NodeID");
                dt.Columns.Add("Coords");
                dt.Columns.Add("Type");
                dt.Rows.Add("A", "x1,y1", "Valve");
                dt.Rows.Add("B", "x2,y2", "Valve");
                dt.Rows.Add("C", "x3,y3", "Valve");
                dt.Rows.Add("D", "x4,y4", "Vertex");
                dt.Rows.Add("E", "x5,y5", "Valve");
                DataTable dt2 = new DataTable();
                dt2.Columns.Add("Fromnode");
                dt2.Columns.Add("Tonode");
                dt2.Rows.Add("A", "B");
                dt2.Rows.Add("B", "C");
                dt2.Rows.Add("C", "D");
                dt2.Rows.Add("D", "E");
                List<Coords> result = new List<Coords>();
                // Make sure the NodeID is unique
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    //Search the Fromnode's row in dt according to dt2's Fromnode value
                    DataRow[] Fromnodesrow =
      dt.Select(string.Format(" NodeID = '{0}'", dt2.Rows[i]["Fromnode"]));
                    //Search the Tonode's row in dt according to dt2's Tonode value
                    DataRow[] Tonodesrow =
      dt.Select(string.Format(" NodeID = '{0}'", dt2.Rows[i]["Tonode"]));
                    // If Fromnode's Type is Vertex not add anyting into result
                    if (Fromnodesrow[0]["Type"].ToString() != "Vertex")
                    {
                        //Check the Tonode's value is Vertex
                        if (Tonodesrow[0]["Type"].ToString() == "Vertex")
                        {
                            List<string> temp = new List<string>();
                            temp.Add(Fromnodesrow[0]["Coords"].ToString());
                            temp.Add(Tonodesrow[0]["Coords"].ToString());
                            //search the Fromnode according to Type Vertex Tonode
                            DataRow[] middlerow =
                              dt2.Select(string.Format(" Fromnode = '{0}'", Tonodesrow[0]["NodeID"].ToString()));
                            DataRow[] Tonodesrow2 =
                            dt.Select(string.Format(" NodeID = '{0}'", middlerow[0]["Tonode"]));
                            temp.Add(Tonodesrow2[0]["Coords"].ToString());
                            Coords c1 = new Coords();
                            c1.coords = temp;
                            result.Add(c1);
                        }
                        else
                        {
                            List<string> temp = new List<string>();
                            temp.Add(Fromnodesrow[0]["Coords"].ToString());
                            temp.Add(Tonodesrow[0]["Coords"].ToString());
                            Coords c1 = new Coords();
                            c1.coords = temp;
                            result.Add(c1);
                        }
                    }
                }
                int ia = 0;
            }
            public class Coords
            {
                public List<string> coords { get; set; }
            }
         }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2016 3:04 PM