Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
currently i'm facing the problem with nested linq.

here is my tables and now i am developing MVC4 Razor web application.
student
student_id,studentname,description
book
book_id,student_id,bookname,checkitout
booker
bookerid,book_id,bookername,bookerdescription

i create the model for displaying
C#
public class student_model{
	public int student_id {get;set;}
	public string studentname {get;set;}
	public string bookname {get;set;}
}

i want to write nested LINQ for booker. so i use the following LINQ:
C#
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;
}	


It is wrong nested LINQ. so how should i do it to make a filter bookers.book_id = bk.book_id ? and how should i return (List<student_model)?>

Thanks
frog
Posted

1 solution

You need to do a three way join, you can't put LINQ code inside the new block, that needs to just be a constructor for your object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900