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

  • Home
  • SEARCH
  • 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 8205885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:26:47+00:00 2026-06-07T08:26:47+00:00

I am trying to learn Entity framework. Say, I have the following classes class

  • 0

I am trying to learn Entity framework. Say, I have the following classes

class Course
{
  [Key]
  public virtual int CourseID {get; set;}
  public virtual string CourseName {get; set;}
}
class CourseDBContext:DBContext
{
   public DbSet<Course> Courses{get;set;}
}

Then I can use Linq to query the database as shown below

using (CourseDBContext a = new CourseDBContext())
{
  var b = from c in a.Course
          where c.CourseID == 1001
          select c;

  var d = b.FirstOrDefault();
  if(d != null)
    Console.WriteLine(d.CourseName);
}

This works fine. Now if I add a second class

class Assignment
{
   [Key]
   public virtual int CourseID {get; set;}
   public virtual int StaffID {get; set;}
}
class AssignmentDBContext:DBContext
{
   public DbSet<Assignment> Assignments{get;set;}
}

Now, How can I use Linq to select and display the CourseName and StaffID associated with CourseID = 1001?
The example above is contrived and so the table design and fields are irrelevant. I just want to know how to query the data between two classes from two different database tables using Entity Framework and Linq.

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-06-07T08:26:48+00:00Added an answer on June 7, 2026 at 8:26 am

    Both entities need to be in the same context.

    public class CoursesContext: DbContext
    {
       public DbSet<Assignment> Assignments {get; set;}    
       public DbSet<Course> Courses {get; set;}
    }
    

    You can add an Assignment navigation property to filter on a foreign key:

    public class Course
    {
      [Key]
      public virtual int CourseID {get; set;}
      public virtual string CourseName {get; set;}
      public virtual Assignment {get; set;}
    }
    

    Then you can query like so:

    var staffId = 
          from c in a.Course
          where c.CourseID == 1001
          select c.Assignment.StaffID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn how Entity Framework works. I understand that EF SaveChanges
I'm trying to learn ASP.NET MVC + Entity Framework by developing a small project.
Let's say I have an Entity in my Entity Framework Model that is named
I'm brand new to the Entity Framework and trying to learn all it can
i'm trying to learn Entity Framework and I need some help to build my
Trying to learn about php's arrays today. I have a set of arrays like
I'm trying to learn about grails with Google App Engine and JPA by following
I was trying learn lex and yacc using the oreilly book. I tried following
I am trying to learn how to use the AsyncTask class to authenticate the
trying to learn and practice arrays but I have a problem with this small

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.