Entity Framework - 用数据库中的数据填充

Entity Framework - Fill with data from database

我正在尝试用我的数据库中的数据填充邻接矩阵,但我完全迷路了。

我知道如何正常使用但不知道 Entity Framework。

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: 'Input string was not in a correct format.'

看起来你正在循环遍历矩阵。我会遍历数据行并在那里做矩阵的事情:

// 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);

}