How can I make this LinqToSQL query work? (SqlExecption)

Posted by kversch on Stack Overflow See other posts from Stack Overflow or by kversch
Published on 2010-06-03T09:02:43Z Indexed on 2010/06/03 9:24 UTC
Read the original article Hit count: 286

Filed under:
|

The following code gives me an SqlException:

OO theCourse = subject.Course;
var students = dc.studentsCourses.Where(x => x.course == theCourse).Select(x => x.student);

"Invalid object name 'dbo.studentsCourses'."

I tried the following code instead but I also get an Exception. My original question was asked on Aardvark and can be read bellow:

var allStudents = from s in dc.students select s;
List thestudents = new List();
foreach (student s in allStudents)
{
     if (s.courses.Contains(theCourse))
     {
     thestudents.Add(s);
     }
}

I did a right click, "run custom tool" on my dbml and checked my names of my tables and entities. The project compiles but I get an Exception at runtime on this line: "if (s.courses.Contains(theCourse))" Any ideas?

Original question on Aardvark:

How do I do a LinqToSQL query that gives me this: I want to select all students that attended a certain lesson. The lesson is from a certain course. So select the course the lesson is from. Now select all the students that are following that course. There is a many-to-many relationship between the students and the courses table in my DB. I already extended my LINQ entities to be able to select student.Courses and course.Students using this method: http://www.codeproject.com/KB/linq/linq-to-sql-many-to-many.aspx

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql