我正在尝试从数据库中的数据填充邻接矩阵,但是我完全迷路了。
我知道如何正常使用实体框架,但是不知道。
This is the graph
如第一行所示,IdNodoInicial-IdNodoFinal
是1-> 2。
查询数据时我该怎么做?
foreach (int x in MatrizAdyacencia)
{
int ini = Convert.ToInt32(grafosdb.Aristas.Select(y => y.IdNodoInicial).ToString())-1;
int fin = Convert.ToInt32(grafosdb.Aristas.Select(y => y.idNodoFinal).ToString())-1;
MatrizAdyacencia[ini,fin]= Convert.ToInt32(grafosdb.Aristas.Select(y => y.Costo));
MatrizAdyacencia[fin,ini] = Convert.ToInt32(grafosdb.Aristas.Select(y => y.Costo));
}
到目前为止,这是我遇到的错误
System.FormatException:'输入字符串的格式不正确。'
答案 0 :(得分:0)
好像您正在遍历矩阵。我会遍历数据行,并在那里做矩阵的事情:
// grab the rows you want, this would be all
var aristaList = grafosdb.Aristas.ToList(); // unless Aristas is a List from Repo?
foreach (var arista in aristaList)
{
// not exactly sure what you are dong here, but equivalent would be
int ini = Convert.ToInt32(arista.IdNodoInicial)-1;
int fin = Convert.ToInt32(arista.idNodoFinal)-1;
// populate matrix (assuming it was initialized)
MatrizAdyacencia[ini,fin] = Convert.ToInt32(arista.Costo);
MatrizAdyacencia[fin,ini] = Convert.ToInt32(arista.Costo);
}