2017-11-10 4 views
1

私はC#、SQL Server、ASP.NET、Entity Framework、Linqの各テクノロジを使用しています。学生IDを指定Linq Entity Framework:IEnumerableへの反復データなしでテーブルデータを選択

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; } 
} 

が、私はのリストを返したい:

my database diagram

モデルクラス:

私は、データベース内の多対多の関係を持っています彼/彼女が書かれているコース。

これは私がこれまでにしようとしているものです:

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ブロックを使用しないでください。あなたは(それが透過的に作成されます)あなたのモデルに「リンクテーブル」(OPでinscribe)を追加する必要はありませんコードファーストアプローチで

おかげ

答えて

2

//Models 
public class Course 
{ 
    [Key] 
    public int Id { get; set; } 
    //more properties here 
    public virtual /*important*/ ICollection<Student> studs { get; set; } 
} 
public class Student 
{ 
    [Key] 
    public int Id { get; set; } 
    //more properties here 
    public virtual /*important*/ ICollection<Course> courses { get; set; } 
} 

//Controller 
var stud = _dbContext.studs.Where(s => s.Id == /*param*/id).FirstOrDefault(); 
var courses = stud.courses.ToList();//Real courses with id, name, etc. No Include required 

更新
あなたは「リンクテーブル」を(例えばsortOrderまたはenrollmentDateのようないくつかのプロパティを追加する)必要がある場合、その後のモデルは少し異なるものになります。

//Models 
public class Course 
{ 
    [Key] 
    public int Id { get; set; } 
    //more properties here 
    public virtual /*important*/ ICollection<StudentCourse> studs { get; set; } 
} 
public class Student 
{ 
    [Key] 
    public int Id { get; set; } 
    //more properties here 
    public virtual /*important*/ ICollection<StudentCourse> courses { get; set; } 
} 
[Table("inscribe")] 
public class StudentCourse 
{ 
    [Key, Column(Order = 0)] 
    public int StudentId {get; set'} 
    [Key, Column(Order = 1)] 
    public int CourseId {get; set'} 
    //extra properties 
    [ForeignKey("StudentId")] 
    public virtual Student stud { get; set; } 
    [ForeignKey("CourseId")] 
    public virtual Course course { get; set; } 
} 

//Controller 
var courses = _dbContext.courses.Where(c/*c is "link"*/ => c.Student/*StudentCourse prop*/.Any(s/*link again*/ => s.StudentId == someId/*param*/));//again courses 

ご覧のとおり、Includeは必須ではありません。

+0

よろしくお願いします。私はあなたがコントローラの中に示唆しているコードが刻印テーブルを逃していることに気づいた。ありがとうございます –

+0

@evvk更新 –

+0

更新をありがとう、私は質問があります、私は仮想プロパティを使用すると、私は遅延読み込みを有効にするが、熱心な読み込みであなたの提案を実装することはできますか? –

0
var result = _dbContext 
    .Inscribe.Include(c => c.Courses) 
    .Where(c => c.StudentId == studentId) 
    .SelectMany(c => c.Courses).ToList(); 
+0

こんにちは、私は試しましたが、エラーが表示されます。メソッドの型要素は使い方から推測できません。引数を明示的に指定してみてください。 ありがとう –

関連する問題