Understand your C# queries! IEnumerable & IQueryable in explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey and welcome to tutorial it's EU my name is Yannick and in this video we are going to take a look at the difference between an i queryable and an ie numberable and that's very important because the difference between an i enamorable and an i variable has a huge impact on the performance of your database queries since most of the jobs out there are in the fields of web development you should definitely be interested in optimizing your code as much as possible so let's get started [Music] alrighty so here I got a Net 7 console application with three classes I put all the classes inside one file you can for sure split it up I just do that so that you can follow along in the easiest way possible now let's just real quick it just takes 20 seconds let me just explain you what I got here we got a customer right and that customer has a property and I got a Constructor and that Constructor is well just creating that object instance and setting the revenue so this is our well our entity model so let's just assume we have a database like an SQL database with customers okay and then we get the orm the object relational mapper just think of it like an SQL database and inside that database we have a table called customers and then we have data entries inside of that table which you can see right here just new customers um well and each of them has a unique Revenue value please don't Focus too much on that as I said it's really just a data entry I just tried to build like some nothing looking like in SQL or behaving like an SQL database right so this is really just for providing us any kind of data so notice that we got the orm class here and there is a customer list and that's all we have to do we pre-fill it with some data once we create an instance in the Constructor if we scroll a little bit down in the or I am right here you can see that we have two methods now this is where things are getting interesting we got one method which is returning an i queryable of type customer right get customers as queryable I just named that method so I implemented it and it Returns the customers as queryable so since this is a collection we can simply call the S querable now the other method here is public I enumerable of type customer and it for sure Returns the customers as innumerable so what's the difference between a queryable and an innumerable any numberable is already a collection so let's take a look here at our list if we just select that list and hit F12 we will come into the definition for sure of that class you can see that we have the I list interface right here and if I again hit F12 and get into that implementation you can see the interface and that interface is implementing the IE numberable and that basically means an array a dictionary and a list all of them are enumerables so they are collections they are like a fixed data set right so simply think of it that way if we have an A numberable we have like real data okay so we have a data set maybe it's empty maybe it's not empty but we have an object instance we got something now on the other hand the queryable is nothing real yet it's just like hey please note that I want to get all the customers but you don't have any object instance you don't have any data right it's just like the query so far but it's not executed that's the difference right just a quick note if you like this kind of videos please go ahead and like this video And subscribe to our channel so that you no longer miss any upcoming videos oh and if you're a C sharp developer and you finally want to land your very first job and maybe you watched a lot of online courses already or maybe you have participated in a boot camp but you still are struggling in landing or finding your first job as a developer please check out our c-shock progress Academy it's a unique online course self-paced and it helps you learn the skills that you really need to land your first job this includes technical new skills for example asp.net current angular but also a lot of soft skills for portfolio building for job interviews and all of that you can find the link in the description below or popping up right now at the top right corner now let's continue at our internal class program right here so this is where the external takes place here we got a static void Main and inside of that vote we simply create a new instance of our RRM which contains our data set right our customers so let's get started with the ie numberable I simply create an I enamorability right here of type customer and I will simply call it e for enamorable I know it's not the best name but I would simply call it e and then we're gonna call our database and call get customers as enamorable right so this is the method that we have implemented the buff Great so now e in that moment where this line will get executed e will be let me just comment that here e will be let's say we have 50 000 customers I know in our data set we only have like 20 or something I just used it for visualization but let's assume that we have 50 000 customers in that moment e would be 50 000 customers why because in a numberable is a real collection as I said there is data inside awesome so our next step would be to filter that because we're only looking for high paying customers for example so let's say VAR high paying customers and that's equals to e dot where so we're going to filter it and we filter where each customer so where C goes to C dot revenue and then we're gonna say is bigger than 2500 revenue for example right so this will filter our um our customers for high paying let's say we got 25 000 high-paying customers so the the 50 000 just the half of it so e or a high paying customers now there is a 25 000. so we now have executed two queries the first one was getting all of our customers all of our customers so 50 000 customers were transferred via the network 50 000 already so we got all of our customers just imagine you got like five million customers inside of your database that query here or that call would be like crazy okay and afterwards we got the second call which is filtering them and that may also take a while to get executed right but that one here that's definitely the the big problem because now you got like all of your customers inside of your memory and all of that great so that's the first point that's the way that you should really not go because as I said we have two queries the first one is getting all the information get all customers and the second one is filtering but we can combine it and significantly increase the performance of that query and I will explain you why and how it uh how it's done right now so let's do nearly the same stuff with an i queryable instead of an eye enumerable again we have a customer here let's call it Q for query and then we're going to say DB dot get customer as queryable okay that's the second method that we have implemented can show you right now just uh for you to remember we got the customer list as with queryable and customers as the numberable right so as I said if you for example use Entity framework um in asp.net core or something like that then Entity framework will handle that for you and return you enumerables or variables so now we called get customers as queryable and now the interesting part happens so what do you think what size how many entries does q have right now if we have like 50 000 customers what's the size of Q how many entries are inside zero once that line is executed you still have zero no data is fetched yet no single data entry okay so it's just like it's it's just like a note hey I will have to get all the customers that's just what's inside of the query right now so you were building up a query okay so no memory got allocated for any kind of data yet now again we want to get the high paying customers so we take the high paying customers again we take our query and we again want to filter so we're going to say where take exactly the same expression where customer that Revenue goes to oh it's bigger than 2500 there we go so again what do you think is the size or how many entries do we have in high paying customers right now right so we grabbed the data the customers is queryable and we have filtered it so how many people are now inside if we have 50 000 customers and half of it so 25 000 people are our customers are high paying again it's zero it still did not get executed it's just query building so we're still building the query get all the customers filter them right now when or how can we execute that well it's pretty easy we simply have to call to list or to array or something like that it's something that turns the query into a collection and in that moment using the two list or two array call or whatever the query will get executed so we can simply go ahead and add a two list for example here at the end or two array but I want to do it in a new line so let's say for example VAR final data equals to high paying customers which is our query right high paying customers is our query because it's query.where high paying customers dot for example to list and in that moment the query will get executed and the query the real SQL query will look something like that it will say select uh all from customers for example so something like where and then Revenue bigger 2 500 right just something like that so our orm or our database we simply execute one query and only maybe in the end we have like 25 000 customers only those will get transferred via the network so let's assume we only have 5 000 customers which are high paying then only 5 000 customers have to be sent via the network instead of 50 000 just from that single call right here and that's the difference right so just to sum it up in two sentences the I enumerable or what we have right here are two queries which have to run after each other it's getting all the customers and then we're filtering now the second one the I queryable right here is we're building one query get all the information of our customers okay I noted that and then filter it okay I noted that and in the moment we hit to list or two array or something like that in the moment we take the iquarable and turn it into an innomerable in that moment we're going to execute the query inside of our orm and we have a super improved way of querying data and reducing the actual Network traffic awesome so I hope you have learned something you thanks for watching if you liked that video please give it a thumb up and subscribe to our channel so that you no longer miss any upcoming videos
Info
Channel: tutorialsEU - C#
Views: 16,382
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, video course, video tutorial, learn how to, how to, visual studio, c#, .net, .net core, dotnet, visual studio 2019, core, code, asp, asp net, c sharp, ienumerable, iqueryable, coding, linq, entity framework, entity framework core, csharp, iqueryable vs ienumerable, c# ienumerable vs iqueryable, iqueryable .net core, csharp interview, iqueryable vs ienumerable c#, ienumerable vs iqueryable c#
Id: 6V1MSItlqtw
Channel Id: undefined
Length: 11min 27sec (687 seconds)
Published: Fri Dec 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.