2013-01-07 3 views
0

현재 중첩 된 linq에 문제가 있습니다.MVC4 면도기의 중첩 된 Linq

여기에 내 테이블이며 지금은 MVC4 면도기 웹 응용 프로그램을 개발하고 있습니다.

student 
student_id,studentname,description 
book 
book_id,student_id,bookname,checkitout 
booker 
bookerid,book_id,bookername,bookerdescription 

은 내가 모두 여기 내 테이블을

public class student_model{ 
    public int student_id {get;set;} 
    public string studentname {get;set;} 
    public string bookname {get;set;} 
} 

안녕을 표시하기위한 모델을 작성하고 지금은 MVC4 면도기 웹 응용 프로그램을 개발하고있다. booker에 중첩 된 LINQ를 쓰고 싶습니다. 그래서 다음 LINQ를 사용합니다 :

public List<student_model> finder (int stuid){ 
    var stubk = (from stu in contx.students 
     join bk in contx.books on stu.student_id equals bk.student_id 
     where stu.student_id == stuid 
     select new { 
      //here is wrong 
      student = from bker in contx.bookers 
       where bker.book_id=bk.book_id 
       select new student_model{ 
        student_id = stu.student_id, 
        studentname = stu.studentname, 
        bookname = bk.bookname 
       } 
     }).ToList(); 

    var next = stubk.Select(md=>md.student) 

    return (List<student_model>) next; 
} 

중첩 된 LINQ가 잘못되었습니다. 그렇다면 필터를 만들기 위해 어떻게해야합니까? bookers.book_id = bk.book_id? 어떻게해야합니까 (List<student_model)?

감사 개구리

답변

1

에 가입 추가 할 수 있습니다.

이 답변은 Entity Framework를 사용하고 있으므로 서로를 가르키는 클래스에 탐색 속성이 있다고 가정합니다 (예 : Books에는 클래스에 Student 속성이 있고 Student에는 해당 클래스에 Books 컬렉션이 있음). 수업).

다음과 같이 LINQ 확장 방법을 사용하여 수행 할 수 있습니다

var studentModelList = new List<student_model>(); 
contx.Students.Include("Books") 
       .Where(stu => stu.student_id == stuid) 
       .Select(stu => stu.Books).ToList() 
       .ForEach(bookObj => 
         studentModelList.Add(new student_model { student_id = bookObj.student.student_id, studentname = bookObj.student.studentname, bookname = bookObj.bookname})) 
return studentModelList; 
0

제거 중첩 및 추가 당신이에 대해 전혀 예약자 클래스를 사용할 필요가 있다고 생각하지 않는다

public List<student_model> finder (int stuid){ 
    var stubk = (from stu in contx.students 
     join bk in contx.books on stu.student_id equals bk.student_id 
     join bker in contx.bookers on bk.book_id equals bker.book_id 
     where stu.student_id == stuid && bker.book_id=bk.book_id 
       select new student_model{ 
        student_id = stu.student_id, 
        studentname = stu.studentname, 
        bookname = bk.bookname 
       }).ToList();