score:18
Updated (post C# 7) Answer
If using C# 7 or 8 then one could use the is
keyword together with Linq.All
:
var result = Comparison.All(item => item.Value is null)
If using C# 9 then one could use the is not null
together with Linq.Any
:
var result = Comparison.Any(item => item.Value is not null)
If using C# 9 then one could also use the is object
or is {}
together with Linq.Any
:
var result = Comparison.Any(item => item.Value is object)
All these options are somewhat equivalent. At least in terms of time complexity they are all O(n)
. I guess the "preferred" option simply depends on personal opinion.
Original (pre C# 7) Answer
Using linq method of All
:
var result = Comparison.All(item => item.Value == null)
Basically what it does is to iterate all items of a collection and check a predicate for each of them. If one does not match - result is false
score:4
You can check by this linq statement
var isNull = Comparison.All(item => item.Value == null);
score:1
I'm not totally sure about the internal differences of All
and Exists
, but it might be a good idea to just check whether one of the entries is not null and then negate the result:
var result = !Comparison.Exists(o => o.Value != null);
I would expect this query to quit after the first non-null value was found and therefore to be a little more efficient.
Update: From the Enumerable.All documentation:
The enumeration of source is stopped as soon as the result can be determined.
Therefore, using All
will probably not result in the entire list getting processed after a non-null value has been found.
So the aforementioned possible performance gain is not likely to occur and both solutions probably do not differ.
Source: stackoverflow.com
Related Articles
- Check null value in a list using linq
- Check if List is not null when using "contains" in LINQ query else select all records
- Using Linq to check if a list includes null values in the middle
- Check if list is null before using linq
- Update List column values, if value is null using Linq
- Check a list of DTOs using linq to see if all values that exclude a particular value all equal the same thing
- How do I check for a null value in a tuple or single value in a column using LINQ for Entity Framework?
- Using LINQ to check for anything in a list that is not null or blank
- how do I check for underlying null value using linq to sql?
- Using Linq to check if generic list in Dictionary contains a value
- Searching if value exists in a list of objects using Linq
- Simplest way to filter value from generic List in C# using LINQ
- How to update value in a List using LINQ
- Check that all items of IEnumerable<T?> has the same value using LINQ
- Linq Select New List Property Null Check
- Best way to check if value exists for a key in ILookup<string, string> using linq
- Get index of matching value in List using LINQ
- assign value to a new list using linq
- Get groups of 4 elements from name value list using LINQ in C#
- Using DefaultIfEmpty in Linq - problem substituting a null value for a default value
- Linq query, how to check for a null value and use the value 0 in place of a null?
- Cast a projected list using LINQ returns a list of null values?
- Linq check for null and replace null value in orderby
- MVC Controller: Using LINQ to check for duplicate value already existing in table before Save?
- sorting list of objects with null properties using linq
- Check if a value from one array exists in another array using linq
- Using Linq to find a value in comma separated value in a List
- Return list with more than one value stored in a dictionary using linq
- Getting the value of an inner list using LINQ
- Get minimum and maximum time value from list of object property using Linq
- Linq query & generics
- Parametrize F# queries based on properties
- LINQ Where statements concatenated with OR
- Object reference not set to an instance of an object model fk and virtual field
- Linq query with 'åäö' returns all items in table
- How do you check if Record exists
- Why is one character missing in the query result?
- Filling a DataSet or a DataTable from a LINQ query result set
- Linq to EF Dynamic Search with multiple properties
- Tranform and Apply Expression via LINQ?
- LINQ to Entities: queryable extension method not reconized inside where condition
- How to get distinct values from nested lists of objects?
- Finding strings that are not in DB already
- if only one record is return linq output is wrong c#
- LINQ: Select a column based on another column value
- How to read SQL binary field in C#
- Simple LINQ DataTable Aggregation Doesn't Work
- Count rows between two cells datagridview using linq
- How to query a sitemap using an XDocument linq query
- linq to update or insert on a csv file