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 3758388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:09:15+00:00 2026-05-19T10:09:15+00:00

I have 3 tables with a many to many relationship between them User: Id

  • 0

I have 3 tables with a many to many relationship between them

User: Id (PK), Name

UserCourses: UserId (PK), CourseId (PK)

Courses: Id (PK), Name

I need to write a linq query to select all the courses name of user X and return in a IEnumerable but I can’t get it to work.

EDIT:

public IEnumerable<Courses> GetCourses
        {
            get
            {
                return (from a in _entities.Users.Include("Courses")
                        where a.Id == this.Id
                        select a.Courses.AsEnumerable()
                        );
            }
        }

Any help much appreciated

Thanks

  • 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-05-19T10:09:15+00:00Added an answer on May 19, 2026 at 10:09 am

    You’re a little unclear about what’s going wrong, but that looks like it would produce an IEnumerable<IEnumerable<Courses>> type. If you’re looking for a flattened IEnumerable<Courses>, which I think you are, you need something like:

        public IEnumerable<Courses> GetCourses
        {
            get
            {
                var query = from a in _entities.Users.Include("Courses")
                            where a.Id == this.Id
                            select a;
    
                return query.FirstOrDefault().Courses;
            }
        }
    

    EDIT: To avoid a NullReferenceException, try this instead:

        public IEnumerable<Courses> GetCourses
        {
            get
            {
                var query = from a in _entities.Users.Include("Courses")
                            where a.Id == this.Id
                            select a.Courses; //note the difference
    
                return query.SelectMany(i => i);
            }
        }
    

    Note that there are more than one ways to do this; for example, you could also use:

                var query = from a in _entities.Users.Include("Courses")
                            where a.Id == this.Id
                            select a;
    
                var user = query.FirstOrDefault();
    
                return user == null 
                          ? user.Courses
                          : null;
    

    If the SelectMany version still causes a NullReferenceException, use this instead. I don’t think it should, but I haven’t tested it. You indicated that Rup’s solution had done that, and he’s using SelectMany in much the same way I am, so this last version plays it safe.

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

Sidebar

Related Questions

I have two domain classes with a many-2-many relationship between them, e.g. User and
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a one to many relationship between two tables. The many table contains
I have two models Property and Amenity the relationship between them is many to
I have 2 tables -- a master and details. It's a 1-to-many relationship between
I have two tables: invoices & invoice_items. I need a one to many relationship
i have 4 tables: questions, answers, users, and inquiries (relationship-table between all of them)
I have in my database 3 tables with many-to-one relationships between them: A *---1
I have two tables (renamed/refactored for illustrative purposes) with a Many-To-Many relationship in an
I have 2 tables, Order and OrderItem, which have a 1-many relationship. When I

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.