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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:25:52+00:00 2026-06-02T19:25:52+00:00

Using C# and LINQ to entities I have a problem with child and parent

  • 0

Using C# and LINQ to entities I have a problem with child and parent entity searching. In conceptual terms I am trying to get a Collection of IEnumerable children where these children have certain properties and also the parents of these children have certain properties.

In concrete terms I have Routes and Streets which have a many to many relationship. I am trying to find Streets on a specific Route where the Street has a positive property of LeftNote or RightNote (LeftNote and RightNote are strings and I am searching for strings that are not empty space).

I have the following entities (cut down for clarity)

public class Route
{
    public int RouteID { get; set; }

    public virtual ICollection<Street> Streets { get; set; }
}

public class Street
{
    public string LeftNote { get; set; }

    public string RightNote { get; set; }

    public virtual ICollection<Route> Routes { get; set; }
}

I have the following LINQ expression:

var streets = this.repository.Routes
                        .Where(r => r.RouteID == routeId).FirstOrDefault()
                        .Streets
                        .Where(s => s.LeftNote.Length > 0 || s.RightNote.Length > 0);

This works perfectly until I run this against entity data where the Route exists but there are no streets that have LeftNotes or RightNotes. In these cases I get a NullReference exception. I am trying to find a way to properly express this query which handles the absence of Streets with LeftNotes (there should always be a Route which matches routeId. If not that is a valid exception case and should throw and error).

EDIT: The issue seems to be around null strings rather than anything related to the LINQ construct.

  • 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-02T19:25:57+00:00Added an answer on June 2, 2026 at 7:25 pm

    Do you actually mean

    var streets = repository.Streets.Where
           (s => s.Routes.Any(r => r.RouteID == routeId )
                 && (s.LeftNote.Length > 0 || s.RightNote.Length > 0));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I am using the Linq entity framework. I have 2 entities: Content and
I'm trying to load a collection of entities using Linq2SQL. The problem is, I
Using LINQ to Entities with Entity Framework and C#: I have a method that
I've been working around a problem I have when using LINQ to Entities when
I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it)
Possible Duplicate: 'Contains()' workaround using Linq to Entities? Using LINQ in C#, I have
I have this project set up with EF4 and I'm using LINQ to Entities
I have a very strange problem with a LINQ to Entities query with EF1.
Linq and EF. I'm pretty new so I have some problem to retrieve entities
I'm having a bit of a problem trying to refine/refactor a LINQ to Entities

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.