开发者

Using Entity Framework how can I retrieve a collection from a table?

Here are the tables:

Using Entity Framework how can I retrieve a collection from a table?

I need to retrieve a collection of Student objects, so I can access their names, last names, etc.

I'm kind of stuck so any help is very welcome, I'm always trying to learn and apply new things to make them stick. :)

So far, I've tried this:

private void cmbGradeParalelo_SelectedIndexChanged(object sender, EventArgs e)
{
    int gradeParaleloId = Convert.ToInt32(cmbGradeParalelo.SelectedValue);

    using (StudentRepository studentRepo = new Stu开发者_如何学GodentRepository())
    {
        studentRepo.FindAllStudents().Where(s => s.GradeStudents
    }
}

.GradeStudents is an EntityCollection<GradeStudent> so I think I can query the data using Linq, but I don't know how. I've never done something like this. :)

Thank you for your time!


Yes, you can query with Linq. There is one beginner caveat, though, that being the need for Include:

Context.Students
       .Include("GradeStudents")
       .Include("GradeStudents.GradeParalelo")
       .First<Student>(s => s.StudentId == 1)
       .GradeStudents

etc.

If you provide a little more information on what you are trying to do, I might be more help.

Note also that if your join table (GradeStudents) did not have extra columns, it would be an Assocation, and you would have access to GradeParalelo directly from Student.


You may find one of these tutorials helpful, depending on whether you're using MVC or Web Forms

MVC / Code First http://www.asp.net/entity-framework/tutorials/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

Web Forms / Database First http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-–-getting-started-part-4


if your tying to get all Students with at least one GradeParalelo then that would work

studentRepo.FindAllStudents()
    .Where(s => s.GradeStudents.Any(gs => gs.GradeParaleloId == gradeParaleloId));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜