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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:28:46+00:00 2026-06-07T01:28:46+00:00

I am using Entity Framework and have the following classes class Student { [Key]

  • 0

I am using Entity Framework and have the following classes

class Student
{
 [Key]
 public virtual int StudentID {get; set;}
 public virtual string StudentName {get; set;}
 public virtual ICollection<Note> Notes {get; set;}
}
class Note
{
 [Key]
 public virtual int NoteID {get; set;}
 public virtual int StudentID {get; set;}
 public virtual string Message {get; set;}
}
class StudentDBContext:DbContext
{
 public DbSet<Student> Students { get; set; }
 public DbSet<Note> Notes { get; set; }
}

So to summarize, I have a class of students who can each have many notes. Now, I want to use Linq to retrieve and display all the notes for a particular student. So I try

using (StudentDBContext a = new StudentDBContext())
{
 var b = from c in a.Student
         where c.StudentID == 1001
         select c;

 var currStudent = b.FirstOrDefault();
 Console.WriteLine(currStudent.StudentName);

  //display all the messages of the current student
  foreach (var currNote in currStudent.Notes)
  Console.WriteLine(currNote.Message);
}

In the above code, my foreach block always fails because Student.Notes is always null. Am I missing some step in initializing Student.Notes and populating it from the database?

  • 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-07T01:28:47+00:00Added an answer on June 7, 2026 at 1:28 am

    try this:

    using (StudentDBContext a = new StudentDBContext())
    {
       var b = ((from c in a.Student
               join b in a.Notes on b.StudentId equals c.StudentId into sNotes
                     from notes in sNotes.DefaultIfEmpty()
               where c.StudentId==1001
               select c).SingleOrDefault();
       if (b != null )
         ...
    }
    

    Be aware of the fact that Entity Framework will do a lazy load of your data. So if you don’t do something to populate it then you will not get it. There are other ways of doing it but among other things this query clearly documents that you are wanting both the student and notes.

    • 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 Entity framework. Say, I have the following classes class
I have the following problem. I am using Entity Framework to connect to a
Trying to map the following schema using the Entity Framework. A Customer can have
I am using EntityFramework 4.1. I have the following model: public class User {
I am using RIA services with Entity Framework- I have the following sql select
I have a problem with the following Linq query using Entity Framework: from o
Here is my problem in C# : I have the following classes: public class
Using Entity Framework 4.1 I have the following tables I'm using with Entity Framework
I am using Entity Framework Code First Approach. I have following code to insert
If i have the following entity: public class PocoWithDates { public string PocoName {

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.