IEnumerable vs IQueryable C# | C# Interview Questions | Csharp Interview Questions and Answers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll try to demonstrate what is the difference between ienumerable and iqueryable now both these interfaces you know they are interfaces for dotnet collections so in case you are new to dotnet collections what my suggestion is to go and watch the collections videos where we have like five to six videos and in one of the videos we have already discussed this I in emeral interface so before you move ahead with this videos what my suggestion is to go and watch that video you know where we are discussed about I enumerable but in this video we will concentrate on the differences between ienumerable and iqueryable now let me start this video by putting down two important points the first point is both ienumerable and iqueryable are interfaces to.net collection and the second point is which is very important i queryable inherits from dot from ienumerable again i am repeating iqueryable interface inherits from ienumerable in other words whatever things i enumerable can do iqueryable can also do but there are some things which i queryable can do but i enumerable will not be able to do it now there are lot of differences between both of these interfaces lot of small differences but let me this you know in this video let me talk about that one big difference which will make a difference so rather than going and talking about ten differences I will talk about that one big difference between both of these interfaces this will change your thought when to use ienumerable and when to use iqueryable now here I have a very very simple entity framework code now in case you're new to entity framework please do go and see the video of entity framework questions and answers on crisp on video comm where we explain right from scratch still a professional level how to use entity framework but at this moment you know I would like to concentrate on on pointing out the difference between ienumerable and iqueryable rather than going into entity framework okay but at this moment this entity framework code what it does is it actually goes and loads the employees database the employees table from here right into this I collection okay so in this first line here by using EF you know we are loading the employee table right and just in the next line that is this line I am going and applying a filter you are saying that please filter for me EMP ID filter formula the EMP ID which has value to okay so in the second line here I am going and applying a filter now let me go and put a debug point here and let me run this code in a debug mode and what my interest is that basically I would like to go and see that what kind of sql's are getting fired on MySQL server because this entity framework right so when I'm applying a filter here it will go and fire SQL at that at the back end you know at the end of the day he will fetch the data from SQL Server right so what I will do is let me go and run my profiler here in case again your new if you in case you're new to profiler what my suggestion is to go and watch the videos on SQL Server performance tuning in a very explain what exactly is SQL profiler but in short in a SQL profiler helps you to understand you know how the sql's are getting fired on your SQL server so what I'll do here is I'm going to go and run this profiler so you can see that I have started in a profiler and I will say run here okay so there my profile is running at the background I'll just go and clear everything at this moment here and let me go back to my code here and you can see now I am I am on that code you know where the filter is getting applied okay so I have a I in available collection and on that ienumerable collection we are applying a filter of EMP ID equal to two now if I go and execute this right so I've executed this line of code here let me let me go back and switch back to my profiler here very quickly so you can go back to my profiler here and let me go and stop profiling to see that what kind of SQL it has fired and you can see that here is SQL very quickly let me point the scale out to you you can see that he is actually fetching all the data from the TBL EMP name table my expectation was that he should say here where EMP ID equal to two in other words what's happening here is all the data from SQL Server is going to the client to my console application and then the filter is getting applied on it right so when you use ienumerable and when you apply a filter on it the full data will go from SQL server to your client side and then the filter will get applied now what I'll do is let me go back to my code here and let me change this I enumerable - I variable so let me go here and change this to I variable let me run this application again let me go back here and go back to my SQL profiler and let me restart my or let me go and start listening at my profile so I was first going to clear everything what I have here and it will start my profiler here and now let me go and fire the filter so I'm going to go and fire this filter here now let me go back to my profiler to see that what kind of thing it is doing here and there it is you can see now when i used i queer abel and when I fired you know the filter on it right you can see that he has created a SQL accordingly in other words now only the necessary data will come from the SQL server to the client side you can see over here he is saying where EMP ID equal to - right in other words when we use ienumerable the and when you apply filter on it the complete data from SQL Server is sent to the client side and then the filter is applied in memory while in iqueryable the filter gets applied on the school server or on the AR DBMS and one li the necessary data is sent to the client side so the biggest difference between iqueryable and ienumerable is all about where the filter logic gets executed in case of ienumerable the filter logic gets executed on the client side in case of iqueryable the filter logic gets executed on the database side by using SQL so as a practice if you are working with in-memory data collection in memory data collection means that can action has no connection with our DBMS then it is better to use ienumerable but if you are working with collection which is connected to database then iqueryable is a better choice why because it will reduce network traffic and it will also utilize the power of SQL right so I hope that this video was useful in this video we were trying to understand in what scenarios we should use ienumerable and in what scenarios we should use iqueryable so next time you know when you are thinking about using ienumerable or iqueryable just ask one question to yourself is this collection connected to database or is this collection in memory
Info
Channel: Questpond
Views: 142,191
Rating: undefined out of 5
Keywords: c#, ienumerable, iqueryable, c# programmers, c# ienumerable, c# interview questions and answers, csharp tutorial, learn c#, queryable, enumerable, c# basics
Id: yfPGFapabAY
Channel Id: undefined
Length: 7min 21sec (441 seconds)
Published: Sat Nov 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.