我使用一個外鍵來記錄一個Event(=運動規則)的POCO類。 記錄可以鏈接到1個事件,但事件可以有多個記錄(世界紀錄,奧林匹克記錄等)。 現在我想用for-each循環在索引視圖中打印數據庫中的每個事件(+附加記錄)。從數據庫加載不同的值到List <>
但由於表記錄還沒有List屬性,我必須從我的記錄表創建一個本地(獨特)事件列表,這就是我的問題。
有關更多信息,這裏是我的記錄模式
public class Record
{
[Key]
public int RecordId { get; set; }
public string ResultName { get; set; }
public int ResultAge { get; set; }
public double ResultPrestation { get; set; }
public System.DateTime ResultDate { get; set; }
public string ResultPlace { get; set; }
public int CountryId { get; set; }
[ForeignKey("CountryId")]
public virtual Country Country {get; set;}
public int EventId { get; set; }
[ForeignKey("EventId")]
public virtual Event Event { get; set; }
}
和事件模型
public class Event
{
[Key]
public int EventId { get; set; }
public String Sport { get; set; }
public String Discipline { get; set; }
public Boolean SchoolcupEvent { get; set; }
public List<Record> Records { get; set; }
}
我寫了一個小片,我結束了活動的女巫的列表中的記錄存在,但這不是一個不同的清單,這就是我需要的。
List<Event> events = new List<Event>();
foreach(var item in db.Records)
{
Event e = db.Events.Find(item.EventId);
events.Add(e);
}
我希望有人能幫助我。
問候