>> Welcome back. So what we've looked at so far is
a pretty good whirlwind tour of some of the LINQ syntax,
and there's a lot more that we can explore.
So where do you want to go next?
>> Yeah, let's scroll down and see what else there is.
>> Okay.
>> I like the way he's going toward the more advanced ones.
>> Yeah. So this action here says eager and lazy query execution.
So what does that mean?
>> That is a great one that a lot of people getting started
with LINQ sometimes find some rather surprising results.
So when I run this one,
you'll see that what we get when we run this query to
select any numbers and then select that value of i,
and when we print this out, we see that v,
which is the value that came out of the query,
is 1 and so as i.
Then as we keep rolling through,
i keeps getting incremented.
What that's telling us is this code
here underneath this query clause actually only
executes down here when we're trying
to look at the results and see what's actually in the query.
We'll contrast that with this one up here
where what we say we want to do is,
I'm going to take that query and I'm going
to immediately pipe it into two list and store it in a list.
>> Yeah, let's run that and see what happens.
>> Let's look at that one and see, yeah.
Now if you look, now we see that this changed.
>> Yeah, why has i 10 in all the results but v has changed.
>> Because in this instance,
we specifically requested that the query has to
execute eagerly to store those results in a list.
So now, we want this full query.
>> Okay. So the query has already run.
So i has been incremented 10 times.
>> Right, and there's a really cool way to show this.
Is if I go back up to this first one and I
say that what I'm going to do is let's just copy this,
and let's just write the results again.
We'll rerun that first query where it's running lazily,
and you can see that as I
executed the second time to rerun the results,
I'm now getting i is 11 through 20.
Now if I do the same thing in the second query
where after we've run it runs,
let's enumerate it again and print it out a second time,
and i stays 10 even through that second sequence.
>> So I don't have to write one LINQ query that looks
like 100 lines of SQL procedures.
>> Right. So what can happen here because
these queries execute lazily in general,
you can compose them out of small little bits
of nice-looking C# instead of making
one big long query that looks like one of
our famous stored procedures in
SQL that was like two or three pages long,
and there's only one guy in the company that understands it.
These compose really nicely and we let
the compiler build it
out and actually just request the results when we want.
>> Okay. So would I use this eager execution of my application?
>> Sometimes. What I do with it is if
I'm grabbing some data that probably never changes,
an example I would use is maybe I have
a database and some of it is storing American addresses.
So one area is all the state codes.
Once I grab those 50 state codes,
I'm never going to want to query
the database again because they're probably not going to change.
I can just store them in memory.
But if I wanted looking at data
that is going to change often, well,
then I would query it leisurely and get
fresh data anytime I make the rerun the query.
The last step on here is reusing the query with new results.
>> Oh, wow.
>> So the concept that we just talked about.
So if we run this one,
what we'll see is I'm running
this query and then we're adding the first numbers.
>> Yeah, and those results change, right?
>> Then we're going to go through,
we're going to change the storage here with this loop,
and then we'll run it on a second run.
You can see that the values have changed.
>> Right.
>> Because of that, we get different results.
We even have more numbers because that where clause up here.
>> Yeah, the source has changed.
>> Right. So since now,
there are more numbers that are less than 3 after we've subtracted
5 or we've changed the sign of
every one of those and made them all negative numbers.
Now, a lot more of them pass that filter,
and then the first one we see the numbers 1, 3, 2,
and 0, and the second one
we see everything because they're all negative.
>> Cool. So I can use this with ToList or
ToArray and I'm in control of this lazy.
>> Lazy and eager. It's all up to you.
>> Execution. Yeah.
>> This one up above where it says worried at ToList,
I could just as easily change that to ToArray,
and now I've stored them in an array instead of a list.
>> Great. So that's really awesome to know that I
can decide when I want my queries to execute.
>> Yeah, you're in control as a developer.
>> Yeah. In our next video,
we're going to talk about an introduction
to IQueryable with Entity Framework.