我有一个问题,其中包含列表属性的对象包含 1 个项目,它不应该(它应该是空的)。
Station 类包含一个边列表(List Edges)。Edge 类有一个 EndStation,它的类型是 Station。
问题是,在从 GetEdgesByStartStation() 返回 IQueryable 后,每个 Edge 上的 EndStation 都包含一个不为空的边列表(List Edges)(应该如此)。列表中的项目是它自己的 Station (edge.EndStation)。我试图用下面代码中的注释来说明它
public IQueryable<Edge> GetEdgesByStartStation(Station station)
{
var edges = db.Edges.Where(x => x.StartStationId.Equals(station.ID));
foreach (var edge in edges)
{
edge.EndStation = db.Stations.FirstOrDefault(x => x.ID.Equals(edge.EndStationId));
//Here the edge.EndStation.Edges would contain 1 item, edge.EndStation
//eg. edge.EndStation = "Miami", edge.EndStation.Edges = { "Miami" }
}
return edges;
}
我想要这段代码做的是让所有的 Station 边缘都传递给方法。对于每条边,将 EndStation 与一个空的边列表连接起来。
车站等级:
public partial class Station
{
public Station()
{
this.Reservations = new List<Reservation>();
this.StationMaintenances = new List<StationMaintenance>();
}
[DataMember]
//[JsonConverter(typeof(Int32))]
public int ID { get; set; }
[DataMember]
//[JsonConverter(typeof(Int32))]
public int TypeId { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public decimal StationLat { get; set; }
[DataMember]
public decimal StationLong { get; set; }
[DataMember]
public bool IsOperational { get; set; }
[DataMember]
public bool IsActive { get; set; }
[DataMember]
public DateTime CreatedDate { get; set; }
[DataMember]
public virtual BatteryStorage BatteryStorages { get; set; }
[DataMember]
public virtual List<Reservation> Reservations { get; set; }
[DataMember]
public virtual List<StationMaintenance> StationMaintenances { get; set; }
[DataMember]
public virtual List<Edge> Edges { get; set; }
[DataMember]
public virtual StationType StationType { get; set; }
}
边缘类:
public partial class Edge
{
[DataMember]
public int ID { get; set; }
[DataMember]
public int StartStationId { get; set; }
[DataMember]
public int EndStationId { get; set; }
[DataMember]
public virtual Station EndStation { get; set; }
[DataMember]
public decimal Distance { get; set; }
[DataMember]
public decimal Time { get; set; }
[DataMember]
public bool IsActive { get; set; }
}
怎么会这样?