【发布时间】:2017-11-10 02:32:20
【问题描述】:
我正在使用以下技术:C#、SQL Server、ASP.NET 和 Entity Framework 以及 Linq。
我的数据库中有一个多对多关系:
模型类:
public class Courses
{
[Required]
public int Id { get; set; }
//more properties here
public student stud { get; set; }
}
public class inscribe
{
[Key]
public intId { get; set; }
//properties here
public student student{ get; set; }
[Required]
[ForeignKey("student")]
public string StudentId{ get; set; }
public Courses Courses{ get; set; }
}
给定一个学生 ID,我想返回一个他/她被登记的课程列表。
这是我迄今为止尝试过的:
public IEnumerable<CursoDto> GetCursosAlumno(Int studentId)
{
//some code here to validate
var x = _dbContext
.Inscribe.Include(c => c.Courses)
.Where(c => c.StudentId == studentId).toList();
// x variable is a list<inscribe>
}
我的问题是我不知道如何访问课程实体并将其作为列表返回,例如:
var result = X.Courses;
return result; //result is a list<courses>
我该怎么做?如果可能,请不要使用 foreach 块。
谢谢
【问题讨论】:
标签: c# asp.net entity-framework linq