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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:47:12+00:00 2026-05-13T07:47:12+00:00

I am writing an asp.net mvc application to learn Entity Framework with and am

  • 0

I am writing an asp.net mvc application to learn Entity Framework with and am running into an issue that I do not know how to handle. For simplicity lets take the following table structure:

Movie  
ID (int, not null, auto increment)  
Name (varchar)  
GenreID (int)

and

Genre  
ID (int, not null, auto increment)  
Name (varchar)

Movie.GenreID is a FK reference to Genre.ID

I have brought across all of the tables using the visual designer in VS 2008 and tried the following Linq query:

IEnumerable<Movie> movieList = from f in dataContext.MovieSet.Include("Genre").ToList();

I can output the data in a view using:

<%= Html.Encode( movieList.Genre.Name ) %>

Everything works just fine until I have an item in the Movie table with a null GenreID. Is there something I can do to this query to make it still be able to output (just leave it blank when applicable) or am I doing something horribly 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-05-13T07:47:12+00:00Added an answer on May 13, 2026 at 7:47 am

    This is probably much more readable than .Include("")

    from f in dataContext.MovieSet
    select new
        {
           Name = f.Name,
           Genre = f.Genre // This effectively performs a join.
           ...
        }
    

    You can also check for the problem that way:

    from f in dataContext.MovieSet
    select new
        {
           Name = f.Name,
           GenreName = f.Genre == null ? "" : f.Genre.Name
           ...
        }
    

    This gives you more flexibility, for instance:

    from f in dataContext.Genres
    select new
        {
            Name = f.Name
            Movies = from movie in f.Movies
                     where movie.Duration > 240
                     select movied
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My
I am writing a web application on Asp.Net MVC 3. Assume that we have
I'm writing a ASP.NET Web Application and I'm running into trouble getting the CSS
I am writing an asp.net MVC app that drives an IPhone application. I want
I am writing an application (Asp.Net MVC) that contains a lot of reusable components.
I am writing a Asp.net MVC 2 application that uses Forms Authentication and currently
When writing my first asp.net MVC application using C#, I see that there are
I'm writing an ASP.NET MVC 3 application with lots of classes which are not
I am writing an ASP.Net MVC application that uses NTLM authentication, so users don't
We are writing an ASP.NET MVC application. By default, if the client browser has

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.