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.
Do you actually mean