score:1
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));
Source: stackoverflow.com
Related Articles
- LINQ to entities Child queries where Parent has certain properties
- Fetch Items in LINQ where child entities fulfill certain conditions
- Linq to Select Parent Objects Where Child Objects Have a Matching Child Object
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- linq how to select the parent from a collection where the parent contains child items in another collection
- Linq to Select Parent Objects Where Child Objects Dont contain value
- How can I fetch child entities as DTO in parent using reusable queries/Expression's with EF code first?
- linq anon object utilizing parent object properties and child object properties
- linq query - get parent entity where at least one child entity is part of list
- Linq return child entities not used in parent entities
- linq lamba how can i query a list of child properties and return the parent
- Select parent but order by certain child with dynamic linq
- Using LINQ on local squence to group by parent and child properties
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value
- How select hierarchy from child to parent using linq for entities or EF Core 2?
- LINQ XML - Select all parent elements where a child has a given value
- Loading concrete child types based on abstract parent properties via LINQ
- How do you populate a parent child data structure from XML where generic List<> objects are in parent and child structures using one LINQ query?
- Dynamic Linq - How to select parent objects from child properties
- LINQ to XML Query to find all child values where parent element name like X C#
- c# linq to entities using method based queries - trying to select where the object appears only once
- Linq ordering both parent and child related entities
- Linq query that results in one field in parent entity being a collection of child entities
- Return a parent entity together with its child entities using LINQ method syntax
- Linq select one of list where one of its child objects properties matches a value
- Linq : Load parent and child entities
- How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()
- Linq Select Certain Properties Into Another Object?
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- LINQ to SQL "aggregating" property
- How can I load the following XML using LINQ-to-XML into a dictionary?
- If Participating in Queryable c# join
- What is the complexity of quicksort implemented with IEnumerable?
- Descendants() with a parameter returns empty result
- XDocument not loading properly
- How to use LINQ to generate new type of objects
- how to avoid nested for loop to solve below problem
- An expression tree may not contain an assignment operator?
- Partial Search on SearchResult collection - Active Directory, all Groups
- Using linq to query a collection, grouping and summing certain items
- How to copy over common base properties using linq
- How to compare duration of two DateTime objects in linq
- How can I transform this SQL query to LINQ?
- Dictionary<Foo, List<Bar>> How to assign the list to a property of the key with LINQ?
- Attempting to perform complicated anonymous methods is failing
- performance issue with System.Linq when subdividing a list into multiple lists
- LINQ to SQL and a running total on ordered results
- Linq - filter on multiple possible equals values
- How can i compare date without time part in linq?