LINQ to entities Child queries where Parent has certain properties
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.
你的意思是吗?
var streets = repository.Streets.Where
(s => s.Routes.Any(r => r.RouteID == routeId )
&& (s.LeftNote.Length > 0 || s.RightNote.Length > 0));
var streets = this.repository
.Routes
.Where(r => r.RouteID == routeId).FirstOrDefault()
.Streets
.Where(s => s.LeftNote !=null ? s.LeftNote.Length > 0 : false
|| s.RightNote !=null ? s.RightNote.Length > 0 : false);
Havn't tested it, but think it should work.
OR
this.repository.Streets(s=>(s.LeftNote.Length >0 || s.RightNote.Length > 0 )
&& s.Routes.routeId==routeId));
只需使用String.IsNullOrEmpty()。
var streets = this.repository
.Routes
.Where(r => r.RouteID == routeId).FirstOrDefault()
.Streets
.Where(s => !String.IsNullOrEmpty(s.LeftNote) ||
!String.IsNullOrEmpty(s.RightNote));
链接地址: http://www.djcxy.com/p/37652.html
上一篇: Linq订购父母和儿童相关实体