score:14
You can use (renamed the collection for reasons of readability):
bool anyFalse = myBools.Any(b => !b);
or
bool anyFalse = !myBools.All(b => b);
both are efficient since they break on the first false
.
If there are complex objects in the collection(as it seerms to be) use:
bool anyFalse = MyList.Any(x => !x.MyBool);
or
bool anyFalse = !MyList.All(x => x.MyBool);
score:5
Yes, it will work because Boolean
is IComparable<Boolean>
but it's awkward and thus it's harder to understand the intent of your code.
Additionally, your code will have to go through the whole sequence just to find out if there's a false
, and it will throw if the sequence is empty.
Use All
instead:
MyList.All(item => item.MyBool)
This will return false
if there's any false
value in the sequence. If the sequence is empty, All
will return true
.
Source: stackoverflow.com
Related Articles
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Can I use the .Min() Linq extension method on a bool type?
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- Linq extension method equivalent for that code in VB.NET
- Using LINQ extension method syntax on a MatchCollection
- Ambiguous call when using LINQ extension method on DbSet<T>
- How to return anonymous type from c# method that uses LINQ to SQL
- LINQ SelectMany and Where extension method ignoring nulls
- LinqPad Linq Include() extension method is not found even after adding references
- LINQ Max extension method gives an error on empty collections
- Extension method for IQueryable left outer join using LINQ
- Average extension method in Linq for default value
- LINQ .Cast() extension method fails but (type)object works
- Using Linq's Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method
- How to create a dynamic LINQ join extension method
- Existing LINQ extension method similar to Parallel.For?
- Is LINQ extension method Where guaranteed to preserve order?
- How to: Use async methods with LINQ custom extension method
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Order of items after using LINQ Select extension method
- Error message "Operator '.' cannot be applied to operand of type 'lambda expression'" when converting a method to Extension Method?
- Sum method in LINQ with long type
- LINQ WHERE method alters source collection
- linq extension method to take elements from the end of the sequence
- Get MethodInfo of Count() LINQ extension method on arbitrary IQueryable<T>
- Linq extension method
- Linq to entities extension method inner query (EF6)
- How can I simplify Linq extension method invokation using reflection?
- ReSharper LINQ extension method formatting
- LINQ: How to do JOIN using the linq extension method style on multiple fields?
- How to bind linq query to a repeater?
- When dynamically assigning values to an ASP DropDownList using LINQ, I get a literal string returned for DataValueField
- LINQ and Static ObservableCollection
- Method to sort List of Lists based on Sublist
- Delete from selected table in ASP.NET MVC
- What is the best way to increase the performance of this code?
- LINQ query with multiple aggregates
- Sort the list of strings after string data was added to the list and add method returns void
- Find the closest DateTime key in Dictionary<DateTime, double>
- DefaultIfEmpty() Causing "System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable'"
- WHERE is not being included in the LINQ-query
- Find all nearby customers within a given distance using longitude and latitude
- Linq to EF Include sub List for List
- Is null-checking on Linq queries idiomatic?
- Value can not be null exception
- Entity framework Select first item in group that satisfies condition or just first item
- how to create a XML::Linq XElement in C++, not C#
- CosmosDB document client - how to implicit add type for query?
- C# Lambda - get distinct Id and append other field value
- Comparing 2 objects and retrieve a list of fields with different values