c# - How can I retrieve a List of entities from a many to many relationship? -
i'm trying retrieve list<student> database.
basically, schema is:
gradeparalelo ---- gradestudent ---- student where gradestudent table holds primary keys gradeparalelo , student. note: gradestudent table has column hold integer. not association table.
here's i've tried far:
colegiodbv2entities db = new colegiodbv2entities(); public list<student> findallstudentsfromgradeparalelo(int gradeparaleloid) { return (db.students .include("gradestudents") .include("gradestudents.gradeparalelo") .where<student>(s => s.studentid == gradeparaleloid)).tolist(); } but it's retrieving single student every gradeparalelo choose. understandably, because i'm comparing studentid gradeparaleloid. this not want.
can suggest way retrieve collection?
image want retrieve students in gradestudent table gradeparalelo id 6.
basically this, in more elegant form:
public list<student> findallstudentsfromgradeparalelo(int gradeparaleloid) { list<student> students = new list<student>(); using(gradestudentrepository repo = new gradestudentrepository()) { var items = repo.findallgradestudents().where(g => g.gradeparaleloid == gradeparaleloid); studentrepository studentrepo = new studentrepository(); foreach (var o in items) { students.add(studentrepo.findstudent(o.studentid)); } } return students; }
how about
return db.gradeparaleloes.single(g => g.gradeparaleloid == gradeparaleloid) .gradestudents.select(s => s.student).tolist();
Comments
Post a Comment