score:1

Accepted answer

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));

score:0

Please try the following:

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

Update

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

Update code is after inserting your new code.

The reason to use false first is to force the validation of second or clause. Not sure whether the following works.

score:1

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));

score:0

Does it have to be executed in one query

IEnumerable<Street> streets = Enumerable.Empty<Street>();
var route = this.repository
                .Routes
                .Where(r => r.RouteID == routeId).FirstOrDefault()

if(route != null && route.Streets.Any())
{
    streets = route.Streets
                   .Where(s => s.LeftNote.Length > 0 
                            || s.RightNote.Length > 0);
}

score:1

Just use String.IsNullOrEmpty().

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

score:0

Try changing the LINQ expression to this

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

Related Articles