From the course: C#: Advanced Practices
LINQ query composition and execution - C# Tutorial
From the course: C#: Advanced Practices
LINQ query composition and execution
- Welcome back. So what we've looked at so far, is a pretty good whirlwind tour of some of the links and tags and there's a lot more that we could explore. So where do you want to go next? - [Mika] Yeah, let's scroll down and see what else there is. - [Bill] Okay, I like the way you keep going toward the, the more advanced ones. - [Mika] (chuckles) Yeah, so this section here says eager and lazy query execution. So what does that mean? - [Bill] 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 one, you know, and so is I, and then as we keep rolling through, I keeps getting incremented. And 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. And 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 lists and store it in a list. - [Mika] Yeah, let's run that. - [Bill] Let's look at that one and see, yeah. And now if you look, now we see that the V changes. - [Mika] Oh, so why is, yeah, why is I 10 in all the results, but V has changed? - [Bill] Because in this instance, we specifically requested that the query has to execute eagerly to store those results in a list. So now, there was this whole query here. - [Mika] Okay, yeah, so the query has already run. So I has been incremented 10 times. - [Bill] 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. - [Mika] Okay. - [Bill] And 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. And now if I do the same thing in the second query, where after we've run it once, let's enumerate it again and print it out a second time. And I stays 10, even through that second sequence. - [Mika] So I don't have to write one link query that looks like those 100 lines of SQL procedures. - [Bill] 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-Sharp instead of making one big, long query that, you know, looks like one of our famous stored procedures in SQL that was like two or three pages long and, you know, 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 eager, so would I use this eager execution in my applications? - [Bill] 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. And so, 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 'cause they're probably not going to change. And 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 lazily and get fresh data anytime I make the, rerun the query. Our last sample on here is reusing a query with new results. - [Mika] Oh wow. - [Bill] So kind of that concept that we just talked about. So, if we run this one, what we'll see is I'm running this query, and then running the first numbers, - [Mika] Yeah and those results change, right? - [Bill] And then we're going to go through it, we're going to change the storage here with this loop, and then we'll run it on a second run. And you can see that the values have changed. - [Mika] Right. - [Bill] And because of that, we get different results we even have more numbers because that we're a clause- - [Mika] And that's because this- - [Bill] Up here. - [Mika] Yeah, the source has changed. - [Bill] Right, so since now there are more numbers that are less than three, after we've subtracted five, or we've changed the sign of every one of those and made them all negative numbers. Now, a lot more of 'em pass that filter. And the first one we see the numbers one, three, two and zero, and the second one, we see everything 'cause they're all negative. - [Mika] Cool, so I can use this with two lists or two array and I'm in control of this lazy- - [Bill] Right, lazy and eager, it's all up to you. - [Mika] Execution, yeah. - [Bill] And this one up above where it says where we did two lists, I could just easily change that to two array and now I'm storing 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. - Yep, 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.
Contents
-
-
Introduction to Language Integrated Query (LINQ)5m
-
Learn LINQ query syntax5m 58s
-
LINQ query composition and execution4m 47s
-
LINQ, Entity Framework, and remote data sources8m 13s
-
Introduction to async, await, and tasks3m 45s
-
Asynchronous task composition3m 47s
-
Handling asynchronous exceptions4m 31s
-
Distinguish asynchronous and multithreading5m 36s
-