在MVC4 Razor中嵌套Linq [英] Nested Linq in MVC4 Razor

查看:195
本文介绍了在MVC4 Razor中嵌套Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

目前我正面临着嵌套linq的问题。



这里是我的桌子,现在我是开发MVC4 Razor网络应用程序。

学生

student_id,学生名,描述

book

book_id,student_id,bookname,checkitout

预订者

bookerid,book_id,bookername,bookerdescription


i创建用于显示的模型

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

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



i希望为预订者编写嵌套的LINQ。所以我使用以下LINQ:


i want to write nested LINQ for booker. so i use the following 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)?>



谢谢

frog



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

推荐答案

你需要进行三向连接,你不能把LINQ代码放在新的块中,只需要成为你对象的构造函数。
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.


这篇关于在MVC4 Razor中嵌套Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆