1
我正在使用MVC 4與EF代碼第一種方法。我有兩個簡單的對象。這些都是他們的POCO類:EF代碼第一:使用Linq與多對多的關係
public class Activity
{
//Primitive Properties
[HiddenInput]
public int Id { get; set; }
[Required]
public int LengthInMinutes { get; set; }
public string AdditionalInfo { get; set; }
[Required]
public bool Archive { get; set; }
//Navigation Properties
public virtual User User { get; set; }
public virtual ActivitySet ActivitySet { get; set; }
public virtual ICollection<Company> Companies { get; set; }
public virtual ICollection<Description> Descriptions { get; set; }
}
public class Company
{
//Primitive Properties
[HiddenInput]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public bool Archive { get; set; }
//Navigation Properties
public virtual ICollection<Activity> Activities { get; set; }
}
現在,我有我通過使用foreach循環遍歷活動泛型列表。循環播放時,我想爲列表中與活動相關的每個公司編寫一個名稱。這是我想出的代碼:
@foreach (Activity a in Model)
{
<p>@a.Companies.Where(d => d.Activities.FirstOrDefault(y => y.Id == a.Id)).Single()</p>
}
不幸的是,當我構建項目時,它給我編譯錯誤。我怎樣才能再訪問元素的細節有很多一對多的關係