IQueryable is executed. // // Returns: // A System.Type that represents the type of the element(s) that are returned when. // the expression tree associated with this object is executed.
AsQueryable is a method that allow the query to be converted to an instance of IQueryable. When you use AsQueryable operator on an existing query and apply further transformations such as applying a filter or specifying a sort order, those lambda statements are converted to Expression trees.
Here is a test that i have setup this evening. It was made to prove something different, but the outcome was not quite as i expected. I'm running a test with 10000 random queries on an IQueryable and while testing i found out that if i do the same on a List, my test is 20 times faster.
IEnumerable vs IQueryable The main difference between IEnumerable and IQueryable in C# is that IQueryable queries out-of-memory data stores, while IEnumerable queries in-memory data. Moreover, IQueryable is part of . NET's System. LINQ namespace, while IEnumerable is in System.
If my driveway is empty, I cannot assert that all cars parked there are red.
Consider the following statements.
S1
: My driveway is empty.
S2
: All the cars parked in my driveway are red.
I claim that S1
implies S2
. That is, the statement S1 => S2
is true. I will do this by showing that its negation is false. In this case, the negation of S1 => S2
is S1 ^ ~S2
; this is because S1 => S2
is false only when S1
is true and S2
is false. What is the negation of S2
? It is
~S2
: There exists a car parked in my driveway that is not red.
What is the truth value of S1 ^ ~S2
? Let's write it out
S1 ^ ~S2
: My driveway is empty and there exists a car parked in my driveway that is not red.
The only way that S1 ^ ~S2
can be true is if both S1
and ~S2
are true. But S1
says that my driveway is empty and S2
says that there exists a car in my driveway. My driveway can not be both empty and contain a car. Thus, it is impossible for S1
and ~S2
to both be true. Therefore, S1 ^ ~S2
is false so its negation S1 => S2
is true.
Therefore, if your driveway is empty you can assert that all cars parked there are red.
So now let's consider an IEnumerable<T> elements
and a Predicate<T> p
. Let us suppose that elements
is empty. We wish to discover the value of
bool b = elements.All(x => p(x));
Let's consider its negation
bool notb = elements.Any(x => !p(x));
For notb
to be true, there must be at least one x
in elements
for which !p(x)
is true. But elements
is empty so it is impossible to find an x
for which !p(x)
is true. Therefore notb
can not be true so it must be false. Since notb
is false, its negation is true. Therefore b
is true and elements.All(x => p(x))
must be true if elements
is empty.
Here's one more way to think of this. The predicate p
is true if for all x
in elements
you can not find any for which it is false. But if there are no items in elements
then it is impossible to find any for which it is false. Thus, for an empty collection elements
, p
is true for all x
in elements
Now, what about elements.Any(x => p(x))
when elements
is an empty IEnumerable<T>
and p
is a Predicate<T>
as above? We already know the result will be false because we know its negation is true, but let's reason through it anyway; the intuition is valuable. For elements.Any(x => p(x))
to be true there must be at least one x
in elements
for which p(x)
is true. But if there aren't any x
in elements
it is impossible to find any x
for which p(x)
is true. Therefore, elements.Any(x => p(x))
is false if elements
is empty.
Finally, here's a related explanation on why s.StartsWith(String.Empty)
is true when s
is a non-null instance of string
:
If the number of the items that return true
is the same as the number of all the items, then return true
. Simple as that:
Driveway.Cars(a => a.Red).Count() == Driveway.Cars.Count()
Related explanation: Why does "abcd".StartsWith("") return true?
"If the collection is empty, a test like this is, at best, undefined. If my driveway is empty, I cannot assert that all cars parked there are red."
Yes you can.
To prove me wrong, show me a car on your empty driveway that is not red.
For anyone familiar with the SQL notion that NULL != NULL, this is unexpected behavior.
This is a quirk of SQL (and not quite true: NULL = NULL
and NULL <> NULL
are both undefined, and neither will match any rows.)
Any()
and All()
are just implementations of the usual mathematical operators ∃ (the "existential quatifier" or "there exists") and ∀ (the "universal quatifier" or "for all").
"Any" means that there exists some item for which the predicate is true. For the empty collection, this would be false.
"All" means that there does not exist any item for which the predicate is false. For the empty collection, this would always be true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With