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: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.
score:4
you can check by this linq statement
var isnull = comparison.all(item => item.value == null);
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- how to store dbcontext.emps.ToList() data in a data table
- Build Dictionary with LINQ
- Give a Specific name to column in Linq
- Get an acronym from a string in C# using LINQ?
- linq query with grouping and conditional result
- MongoDB Array Query
- LinQ OrderBy specific element in sublist
- C# - Pass function to derive object property as argument
- LINQ for XML, having trouble reading in multiple elements of varying occurences
- Get one field value from LINQ query
- What's the performance hit of List.OfType<> where the entire list is that type?
- Should I not use LINQ to objects because Microsoft might change it?
- LINQ Group By Multiple parameters or fields
- Get Users where are not in role admin and simpleuser
- compare two folders for non identical files with SymmetricDifference?
- convert a string to datetime in LinqToEntities (inside the query)
- Sorting a group in datatable based on result in rows
- Cannot initialize type 'class' with a collection initializer because it does not implement 'IEnumerable'
- Linq to SQL valid Contacts in each Customer
- Optimizing A List of athlete's competitions with LINQ query or similar approach
- LINQ create new list from elements of outer and inner list
- Strangely slow: what happened when TakeWhile met AsParallel?
- "Cannot Resolve Symbol Select" Trying to use LINQ with DataRowCollection
- Comparing two excel files for differences
- EF Linq query comparing data from multiple rows
- How do you create Expression<Func<T, T>> given the property names of T to map?
- LINQ/EF declaring a temp variable for performance
- Linq query definition
- What does this LINQ query do?
- How do I convert from entity/property names (conceptual) to table/column names (store)? (Trial code [in VB 2010 and EF 4] uses reflection.)