score:5
Any()
doesn't do what you think it does. Without a lambda expression in Any()
, it will just check if there is any element in the enumerable it is called on.
You either want:
types.Select((x, i) => values[i].GetType() != x).Any(x => x)
or maybe
types.Where((x, i) => values[i].GetType() != x).Any()
score:7
Any
doesn't check what's in your sequence. It looks for if there is Any element in the sequence.So whether you have a sequence of true values of false doesn't matter.
score:8
Enumerable.Any
without argument just checks if the sequence contains elements. If you want to know if there are any true
s you have to use the overload:
bool anyTrue = bools.Any(b => b);
score:3
You need to use this overload of the Any() method. The overload you are using just returns true if the sequence contains stuff, it doesn't care what the values are. To check the booleans and only return if one them is true, your code would need to be
if (types.Select((x, i) => values[i].GetType() != x).Any(x => x))
throw new Exception();
Source: stackoverflow.com
Related Articles
- Why does IEnumerable.Any return True for a collection of False-booleans?
- Why does Enumerable.All return true for an empty sequence?
- Why does IQueryable.All() return true on an empty collection?
- Linq query return true or false
- Does FirstOrDefault return a reference to the item in the collection or the value?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Why does Contains() return false but wrapping in a list and Intersect() returns true?
- Linq Any returns true despite all values in the collection being false
- LINQ Enumerable.All always return True if collection is empty
- I am trying to return a True or false if a given directory contains ".exe"
- C# List.Contains does return true when it should
- How to return correct status code when enumeration of IEnumerable fails. ASP.NET WebApi
- return true or false if at least one entity in list A matches an entity in list B
- .Range return true when it should be false
- Linq to return true or false if a set contains distinct values
- LINQ to return true if collection contains element satisfying predicate
- LINQ object of IEnumerable type does not return a DataTable
- Why does this LINQ statement return null and not a IEnumerable with count=0
- Method to return records true false or all
- What does LINQ return when the results are empty
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- Why does IEnumerable<T>.ToList<T>() return List<T> instead of IList<T>?
- The data source does not support server-side data paging
- Add items to a collection if the collection does NOT already contain it by comparing a property of the items?
- Cannot initialize type '' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
- What does this C# code with an "arrow" mean and how is it called?
- LINQ - Does the Where expression return new instance or reference to object instance
- Does Array.ToArray<>() return the original array if it is the same type?
- Why doesn't the Controls collection provide all of the IEnumerable methods?
- Group a collection and return a Dictionary
- Func<T> in Lambda expression with LinQ to entities
- Reusable LINQ query except for where clause
- TakeWhile, but I want the rest of the input sequence as well
- Summing values across nested lists at each index
- Cast Entity to Implemented Interface in a Generic Method Using LINQ for Entity Framework
- OrderBy ThenBy in F#
- Linq 1 letter search and search with numbers
- Linq Query syntax vs. Method Chain: return type
- How to pass a variable in the query below
- How do I combine these similar linq queries into one?
- Get file from directory with max date in file name
- How can I join two tables in Entity Framework
- Performance issue in IEnumerable type when querying large amount of data with LINQ
- Equal on Linq query
- Use Linq to flatten name/value collection
- Get table-data from table-name in LINQ DataContext
- Guid compare not working on linq where condition
- How to perform left join in linq having multiple table joins
- How can I make this LINQ query?
- Avoid magic number when looking for min value