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 Articles
- Handling null values inside a WHERE with ANY Condition in linq
- 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
- 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
- Linq : How do I test a List<bool> for condition where any one of its values == true?
- 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
- LINQ to Entities: queryable extension method not reconized inside where condition
- C# - using Linq to XML to populate list of objects
- counting occurances of item in IEnumerable using linq in C#
- Recursive Linq Grouping
- How can I return a custom class from a web service?
- Is this query being executed on an in-memory collection, or in the database?
- Get Top x Products using Linq
- Linq Query For Many to Many Relation within Intersecting Table
- Linq Contains Query from Table List
- Convert linq result to name value pair
- Combining two list variables into one operation
- How do I iterate through each column of data in a LINQ to SQL query?
- Why is it deleting from other variable while I am Removing from different variable in C#.net?
- LINQ number of database calls
- Merging of xml documents
- Cannot convert 'System.Collections.Generic.List<long?>' to 'System.Collections.Generic.List<long>
- How do i include more than one property in group by key?
- How to update an element value with the duplicated value?
- LINQ to XML - Trying to select a list of elements by the value of their attributes
- Remove entities from collection
- Get unique items from two lists