我正在使用带有 EF 代码优先方法的 MVC 4。我有两个简单的对象。这些是他们的 POCO 课程:
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>
}
不幸的是,当我构建项目时,它给了我编译错误。然后如何访问具有多对多关系的元素的详细信息