简体   繁体   中英

How to show records from one table based on foreign key value in MVC

I'm trying to create a view containing a list of students attending a particular course. A student can attend more than one course, so I have a students (kursist) table and a course_student (kursus_kursist) table in my database. The course_student table has a student_id set as a foreign key referencing student.student_id.

Controller:

// GET: hold/protokol
public ActionResult Protokol(int? id)
{
var kursus_kursist = db.kursus_kursist.Include(k => k.kurser).Include(k => k.kursister);
return View(kursus_kursist.ToList());
}

I've tried doing:

// GET: hold/protokol
public ActionResult Protokol(int? id)
{
var kursus_kursist = db.kursus_kursist.where(kursus_kursist.kursus_kursist_id = id).Include(k => k.kurser).Include(k => k.kursister);
return View(kursus_kursist.ToList());
} 

But I get a "cannot use local variable 'kursus_kursist' before it is assigned", which I understand, but how would I go about filtering the results, so they only include the records from k.kursister that match the id?

It is supposed to be a lambda:

where(x => x.kursus_kursist_id == id)

here x is of the same type as the kursus_kursist variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM