 # Build Dictionary from 2 tables [Answered]RSS

Last post Oct 04, 2016 03:04 PM by Brando ZWZ

• ### Build Dictionary from 2 tables

Oct 03, 2016 09:24 AM|sunsilk10|LINK

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

• ### Re: Build Dictionary from 2 tables

Oct 04, 2016 03:04 PM|Brando ZWZ|LINK

Hi sunsilk10，

#### 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();
DataTable dt2 = new DataTable();
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["Type"].ToString() != "Vertex")
{
//Check the Tonode's value is Vertex
if (Tonodesrow["Type"].ToString() == "Vertex")
{
List<string> temp = new List<string>();
//search the Fromnode according to Type Vertex Tonode
DataRow[] middlerow =
dt2.Select(string.Format(" Fromnode = '{0}'", Tonodesrow["NodeID"].ToString()));
DataRow[] Tonodesrow2 =
dt.Select(string.Format(" NodeID = '{0}'", middlerow["Tonode"]));
Coords c1 = new Coords();
c1.coords = temp;
}
else
{
List<string> temp = new List<string>();
Coords c1 = new Coords();
c1.coords = temp;
}
}
}
int ia = 0;
}
public class Coords
{
public List<string> coords { get; set; }
}
}```

Result: Best Regards,

Brando