Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:53:20+00:00 2026-06-03T21:53:20+00:00

I have the following model (dumbed down for this question): public class Student {

  • 0

I have the following model (dumbed down for this question):

public class Student
{
    public int ID { get; set; }
    public virtual ICollection<StudentCourse> StudentCourses { get; set; }

     public Student(){
        StudentCourses = new List<StudentCourse>();
    }
}

public class StudentCourse
{
    public int ID { get; set; }
    public string Grade { get; set; }
    public int StudentID { get; set; }
    public int CourseID { get; set; }

    public virtual Student Student { get; set; }
    public virtual Course Course { get; set; }
}

public class Course
{
    public int ID { get; set; }        
    public virtual ICollection<StudentCourse> StudentCourses { get; set; }

    public Course() {
        StudentCourses = new List<StudentCourse>();
    }
}

Everything is linked in my DbContext class and working as expected. I want to find all the students that have taken part in a course so I do:

var query = db.Students
            .Where(s => s.StudentCourses.Course.ID == 11); 

but it fails because for some reason EF can’t see the link between StudentCourse and Course. The error message is:

‘System.Collections.Generic.ICollection’ does not contain a definition for ‘Course’ and no extension method ‘Course’ accepting a first argument of type ‘System.Collections.Generic.ICollection’ could be found (are you missing a using directive or an assembly reference?).

However the following works fine:

var query = db.StudentCourses
            .Where(s => s.Course.ID == 11)
            .Select(s=>s.Students);

Is this a bug or am I doing something wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:53:21+00:00Added an answer on June 3, 2026 at 9:53 pm

    StudentCourses is a list of Courses, you can’t access a single course on it with .Course. In order to do that, you need to use a clause which works on individual records, such as select, where, any, etc. (most of the IEnumerable extensions taking a lambda expression do so for this purpose.)

    var query = db.Students.Where(s => s.StudentCourses.Any(r => r.Course.CourseID == 11)); 
    

    If you want to work off of db.Students, fine, but the second example you posted is probably more semantically close to what you’re doing. StudentCourses represents the connection between students and courses. Your where clause restricts to a specific course, the select clause projects a student course collection over its connected student.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model: public class A { public int? Id {get;set;} public
I have the following model entity: public class ScheduledTask { public virtual int ScheduledTaskId
I have the following model: public class Blog { public int BlogID { get;
I have the following model : class Contract { string ContractID{get;set;} ICollection<Part> Parts{get;set;} }
I have the following model public class Account { public int Id { get;
I have following model: public class FormularModel { [Required] public string Position { get;
I have the following model public class UserViewModel { public String CVR_Nummer { get;
I have the following model : public class Foo { [Key] public int FooID
I have the following model (greatly simplified for the purposes of this question): class
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.