C# Picking Apart Expression Trees - LambdaExpression

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
from the previous videos we have our constant expression for the literal 5 we have our parameter expression for the parameter I we have our greater than which combines the two expressions into a single expression tree and takes the parameter expression on the left and the classics cost expression on the right then all that is left to do is to create a lambda expression with a body of the expression that we've just built this is the body of the expression so the way we do that is the same as we've done with all of our expression types and but instead of saying constant parameter R greater than when you simply say expression dot lambda you'll notice there's two overloads we always use the generic one the generic overload requires a delegate type which is going to represent the lambda expressions delicate type if or had we assigned our lambda expression to a delegate which we saw in the very first video on this playlist we assigned it to a func int bool there's several delegate types we could use and create our own that that I take an int as a parameter and return a bool in but anyway I'm going to grab this and copy that and then paste it as the generic type argument for the lambda function here and then the actual function requires a body and then the parameters for the lambda and the body is the entire lambda or expression here the root node or the topmost uppermost node is the greater than those so we will pass greater than there and then all the parameters there's only one for this lambda expression and it is I and we represented that with this parameter expression i per am so I per am and we're done there now notice expression dot lambda the return type let me control ki here and I can move my mouse now it returns this expression dot lambda returns an expression object of type func int bool well I believe we've seen that before isn't that what we started out with so I'm actually just going to copy this and this one we called test I think this one we will call test two analogous with this one we created up to there so now we have test two which is a lambda expression with greater than as the root node and then greater than has a right on the left operand and and hopefully that's pretty straightforward we're done okay we have test 2 which is identical to test and in the second video on this playlist you saw me do this and I spat out a bunch of garbage or copied a lot of code from the reflector I showed you with a compiler built and I just kind of blasted you with all that information but over the last few videos we actually pick that apart and now we have an expression tree that does absolutely nothing ok this is simply data this these are objects it creates objects and whoops sorry all of these functions here are methods constant parameter greater than lambda they simply return objects out on the heap that are sitting there saying hey I'm a lambda expression hey I'm a greater than I'm a hi I'm a 5 they don't actually do anything it's a lot like a database there's data inside of a database or even in a data file you can open up a file but if you don't do anything with a file or a database it's just there it doesn't do anything it is up for us to look at those objects in RAM and reason about them to do something about them but all it is now is just data sitting out in RAM this is actually quite powerful even though this is dumb data sitting out in RAM it's actually quite powerful I'll show you in there not too far future hopefully I can get around to doing a whole playlist on the entity framework and how that works but I just want to give you a little taste of how the entity framework will use this data you don't actually say oh I see what you're trying to do let me convert this data to a sequel statement that I can send a sequel server and sequel server will run it alright you know we've seen lambda expressions to convert to code and code runs just the same way or we always expect code to run it just calls the method hundred times for each element but but if I turn this into data a sequel server can say all you elements where some eye is creative okay I know what that means I'll do it for you also I'll do a video on that shortly but I just want to show you one other little thing about these expressions that we don't often do but we certainly can I can come down here and remember tests and tests - these are both the same the compiler pretty much generates this code for us but it doesn't a little shorter fashion but it's alright our code is pretty much identical to what the compiler does I want to show you that I can say test or test - either one I can say hey compile all right generate the code the.net code compile your lambda expression into actual code and hopefully you're scratching your head a little bit saying oh wait I mean I thought the c-sharp compiler did all the compiling that's true and when I say compile it's not like we go back to the c-sharp compiler and say compile I can certainly run this code on on computers that don't have the c-sharp compiler installed instead what happens is this compile function runs a pretty intricate algorithm but it examines our lambda expression and uses let me just show you using system dot reflection dot emit alright and it emits or creates missile code ms il code remember we start with c-sharp code and then CSC dot exe converts that to missile code Microsoft intermediate language which then at runtime the jitter JIT ter the JIT converts that to native instructions ones and zeros and that sort of thing well at runtime when I say compile when we're running it says hey emit using this emit library that comes with dotnet emit the code and missile so that the jitter can do its job and actually run the code that we generated so it's kind of like well it is not kind of like it is exactly like we are creating code at runtime we're we're compiling code at runtime some pile times and runtimes just cross kind of like the streams and if you ever watch Ghostbusters that's kind of scary well look at the compiled here control ki compile returns a delegate a func delegate hopefully you're very comfortable funk now that a delegate that takes an int as an argument returns a bool so I could certainly say hey I'm func int bull me deli does that me deli me deli gets test compiled and then I can certainly invoke this delegate just the same way I'm vogue any other delegate let's do three that's what we did in the previous videos and then me deli let's do eight all right three is not less than or three is not greater than five so that will return false and eight is greater than five that will return true let me alt drag down here console dot write line just to prove that we can actually print the results here control f5 and look at that test we were able to compile test this is test up here and we emitted code at runtime I'll hopefully get around to doing a playlist on that and we're able to run that code and we can do the same thing with test - hey compile these objects and RAM in the actual code and sure enough we can run it and get results so that's kind of interesting most of the time we're not doing this is actually kind of slow you could think it's kind of expensive or we're compiling code at runtime that's got to take time at runtime to compile code yeah that's generally not what we're doing with this if we really wanted to get a delegate out of it then forget this whole expression thing let's just assign our lambda expression to a delegate and then the compiler will kick in and just convert it to a method like we've seen a hundred times before this well we generally use this expression for is say hey kate compiler do your magic write all that code so that at runtime we'll create this object structure and then i'm gonna look at this object structure reason about it and do something different like on sequel server i'll show you i using the entity framework in a video not too far out but before I end this video if you remember our original code I think I still have it here I threw it in notepad let me clear this off here as well just clear it off remember this is what the compiler did I even wrote a comment there this is what the compiler did and if you look the compilers hopefully this wasn't too intimidating when I yeah but basically the compiler just said you know can I format let me change the font here see if I can get it to fit just a little bit better yeah the compiler I'll scroll forgive me I got a I'm trying to make this little show up for you so we can compare both of them the compiler just skipped all the intermediate variables remember we made this cost expression the intermediate variable we made this I parameter intermediate variable then we made this greater than and the compiler it kept the expression 3 around that's our it's parameter expression but then I just wrapped it all in like this so you can stop the video here if you want but I'm just gonna do exactly what the compiler did just shorten this so you can kind of get a feel for what's going on we don't need to say Const expression here we could just grab this value that returns our cost expression and paste it right there okay and we're done with that we don't need that temporary anymore the parameter the parameter we need the variable name but what we're going to do instead is grab this creation of the parameter and when we pass that in we use that right here so instead of just assigning it out here we'll just assign it in here and and like so so there you go we do the assignment and we actually pass it to expression dot greater than right there and then we don't need to the greater than anymore because we just passed that whole entire thing and for the body of our landis we'll just dump that right there and now we no longer need this line of code so we can delete that and then we're done so then we have test2 and all this kind of ugly code that I tried to format for you in the previous video but let me just show you that we can still run it still compile it test do don't compile give the same results so that's why the compilers version was just so scary is it it doesn't like to do all those temporary variables it just as hey let's just take the return value of all those expressions and pass them in immediately where we need them but anyway that's probably more than you wanted out of this video
Info
Channel: Jamie King
Views: 12,445
Rating: 4.8630137 out of 5
Keywords: C# (Programming Language), Lambda Expressions, Anonymous Function, Expression Trees, Language Integrated Query (Programming Language)
Id: K_8OAHEC73I
Channel Id: undefined
Length: 11min 0sec (660 seconds)
Published: Tue Jul 02 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.