Nemo
Nemo

Reputation: 4721

How to do join in ADO.NET Entity Framework

I am new to Entity Framework. I have a model as given below.

enter image description here

Since I need to access this from Silverlight I have added a DomainService

[EnableClientAccess()]
    public class DomainService1 : LinqToEntitiesDomainService<TESTDBEntities>
    {
        public IQueryable<Lecture> GetLectures()
        {
            return this.ObjectContext.Lectures;
        }      
        public IQueryable<ProjectGroup> GetProjectGroups()
        {
            return this.ObjectContext.ProjectGroups;
        }
        public IQueryable<Student> GetStudents()
        {
            return this.ObjectContext.Students;
        }
    }

I need to add a method to the domain service that returns List<Student> for a given project group id.

List<Student> GetStudentsByProject(int pgid)

OR

IQueryable<Student> GetStudentsByProject(int pgid)

Since this involves a join, I guess I have to change the Model.edmx as well as DomainService.cs file manually. How can I do this?

Upvotes: 2

Views: 972

Answers (2)

Nils Magne Lunde
Nils Magne Lunde

Reputation: 1824

You shouldn't have to change the .edmx file.

public List<Student> GetStudentsByProject(int pgid)
{
    return this.ObjectContext.ProjectGroup.Where(pg => pg.pgid == pgid)
               .SelectMany(pg => pg.Students).ToList();
}

Upvotes: 2

Dennis Traub
Dennis Traub

Reputation: 51624

Just add another method to your domain service and you should be fine:

public IQueryable<Student> GetStudentsByGroup(int projectGroupId)
{
    return this.ObjectContext.Students
               .Where(x => x.ProjectGroup.pgid == projectGroupId);
}

Upvotes: 1

Related Questions