2012-01-04 16 views
2

Entity Frameworkを初めて使用しています。私は以下のようなモデルを持っています。私はSilverlightのからこれにアクセスする必要があるのでADO.NET Entity Frameworkでの結合方法

enter image description here

私は与えられたプロジェクトグループIDのList<Student>を返すドメインサービスにメソッドを追加する必要が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; 
     } 
    } 

を追加しました。

List<Student> GetStudentsByProject(int pgid)

OR

IQueryable<Student> GetStudentsByProject(int pgid)

これは参加を伴うため、私は手動でModel.edmxなどDomainService.csファイルを変更する必要がありますね。これどうやってするの?

答えて

2

.edmxファイルを変更する必要はありません。

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

はおそらく、サブを含む非常に複雑なSQL文になります - 共用体の文を選択します。単純な 'ObjectContext.Students.Where(x => x.ProjectGroup.pgid == projectGroupId)'と比較すると、ca. 7回(!)多くのSQLコード。 –

+0

私は、 'Select'の代わりに' SelectMany'を使うようにクエリを変更しました。これははるかに複雑でないSQLを生成します。 –

+0

与えられたプロジェクトグループIDの生徒を直接取得するだけではどうですか? –

1

ちょうどあなたのドメインサービスに別のメソッドを追加して、あなたは問題ないはずです:あなたの例のようにProjectGroupにクエリを通じて、学生の選択

public IQueryable<Student> GetStudentsByGroup(int projectGroupId) 
{ 
    return this.ObjectContext.Students 
       .Where(x => x.ProjectGroup.pgid == projectGroupId); 
} 
関連する問題