如何返回关于连接到另一个表中的外键的表的所有信息(通过IQueryable)?

时间:2015-07-01 11:53:22

标签: entity-framework web-services asp.net-web-api ef-code-first asp.net-web-api2

public IQueryable<Student> GetStudents()
{
    var balance = (from StandardX in db.Standards
                   join StudentX in db.Students
                   on StandardX.StandardId equals StudentX.StandardRefId
                   select new { StudentX.StudentID, StudentX.StudentName, StandardX.StandardName });

    return balance; // I can't handle that. how can I return? Please help.
}

问题:

我有两个分别名为standardstudent的表。我想通过外键关系检索所有学生信息。我到处搜索这个主题,但没有找到任何合适的解决方案。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

如果您已配置关系,请尝试使用include语句(假设标准是父级和1:1,否则翻转和/或添加集合):

   var balance = (from StandardX std in db.Standards.Include("Student")
                  select new { std.Student.StudentID, std.Student.StudentName, std.StandardName });

https://msdn.microsoft.com/en-us/library/bb896272.aspx