score:2
how about:
y => r.report.Description?.ToLower().Contains(y) ?? false
This uses the null propagation operator (?
) to avoid NullReferenceException
in the case of Description
being null
and if it's null
then we use the null coalescing operator (??
) to provide a default value (false
in this case).
btw you can merge both the Any
calls into one:
searchQry.Any(r => r.report.Description?.ToLower().Contains(y) ?? false
|| r.report.Title.ToLower().Contains(y))
if you want to apply the criteria to each and every Description
first before applying it on Title
then you'll need to stick with your approach of two individual Any
calls:
.Where(r => searchQry.Any(y => r.report.Description?.ToLower().Contains(y) ?? false)
|| searchQry.Any(y => r.report.Title.ToLower().Contains(y))).ToList();
Another approach being to use a Where
clause before Any
to filter out the records where the Description
is null:
searchResult = x.reportsInfo
.Where(r => searchQry.Where(r => r.report.Description != null)
.Any(y => r.report.Description.ToLower().Contains(y))
||searchQry.Any(y => r.report.Title.ToLower().Contains(y)))
.ToList();
Source: stackoverflow.com
Related Query
- Handling null values inside a WHERE with ANY Condition in linq
- Linq : How do I test a List<bool> for condition where any one of its values == true?
- how to check null values in linq left join condition insted in where condition
- How to handle null values in LINQ with multiple where clauses
- LINQ query does not return any results when using with where condition
- Left outer join with multiple where condition with LINQ giving null reference error
- Handling null values for C# linq where query
- LINQ Where with AND OR condition
- LINQ Order By Descending with Null Values on Bottom
- Handling null results with the LINQ Average() method
- Delete inside foreach with linq where
- Linq query works with null but not int? in where clause
- Linq where clause with multiple conditions and null check
- Linq to Dictionary<string, List<string>> with Null values
- Linq to SQL: Where clause comparing a Nullable<DateTime> with a SQL datetime null column
- Linq to Sql with lambda sum as a where condition
- Left/outer join with linq on c# with where condition clause
- Linq with where clause in many-to-many EF Code First object
- Linq to DataSet - Handling Null Values
- How to concat strings in LINQ while properly dealing with NULL values
- C# LINQ GroupBy with NULL values
- LinQ with Count and Where condition
- Linq query and multiple where clauses with OR only first condition gets evaluated
- Convert Dictionary<int, int?> to Dictionary<int, int> by skipping null values with LINQ
- Linq Query Handling Null Values
- C# - Linq optimize code with List and Where clause
- Condition inside linQ Where condition
- Linq sort with Null values
- Linq Select with Where based on Dropdown values
- Linq to Entities : how to handle null values in database with a contains
More Query from same tag
- Help Understanding Enumerable.Join Method
- LINQ equal instead of Contains
- Stop LINQ to SQL caching
- LINQ filtering on a Dictionary<string, IList<string>>
- nhibernate linq magic, display everything except one record
- linq query: list as input parameter
- Rounding a specific Hour in LINQ to Entities
- How do I do a table join using LINQ and entity framework 6?
- LINQ: Fill objects from left join
- OrderBy selector fails while projecting anonymous type
- Use variable value as field with LINQ
- Change app.config at install time
- LINQ - Select * from XML elements with a certain tag
- How to segmentate an IList<T> to segments of N size, without creating copies and without memory allocations?
- 'And' 'Or' Query in ElasticSearch
- LINQ Query not pulling all records needed
- How to get the result of a `linq` back to a DataTable?
- Inner Join on Entity Framework
- Linq search result by closest match
- How to query in LINQ & Entity Framework the unmapped property
- Create Generic method that returns sort lambda expression
- error "Sequence contains no elements"
- Updating an item using LINQ, not working
- Linq get first or last element when List index out-of-range
- Linq Grouping with a sum
- Check a value from a second table within a LINQ Statement
- How to get value from linq query
- Convert Loop To Linq - Model Creation
- C# Covariance issue
- left hand side of an assignment must be a variable