When to use IEnumerable vs IQueryable?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] or in your project but using the correct one in correct situation can help with performance of your application my name is progen and welcome to dotnet mastery if you love content related to.net programming make sure to subscribe to the channel that way you will be notified when I publish new videos now let's get back to the big question what is the difference between I enumerable and I queryable I queryable extends the I innumerable interface so anything that you do with a innumerable can also be done with I queryable on top of that there is one big difference that is directly related to Performance of your application let me explain that with an example we have a client and we have database when we use I enumerable to query something in our database it will always return all the records of that table even if you had a wear condition there where you wanted to filter the data when it retrieves the records from database all the records are returned back to the client and then in memory it applies the filter let me elaborate that a little more let's say you have a code where you are retrieving all the books from your database and you have an eye innumerable on that book list when you add the where condition that only retrieve all the books where price is greater than 500. the actual query that gets executed is select all the columns from book there is no where condition that is passed in the query when Entity framework goes to database using your condition once it retrieves all the book then the filter of price greater than 500 is applied in memory so even if you only had one book where the price was greater than 500 it will retrieve all the books from database and then in memory it will iterate through all of them and it will return the book where price is greater than 500. when you work with I queryable client is querying the database with the same condition the filter is applied on the query itself so all the records are not returned back filtering is done in the SQL itself to see that further we have an i query Apple of book list this time and if we use the exact same where condition the query that gets executed is different you can see in the query itself we have the WHERE condition of price is greater than 500. so that is the main difference when you are working with I queryable and I enumerable as an example I enumerable should be used when you want to filter data in memory one example could be when you retrieve a specific data from database you want to apply on that data set and return all three different data sets to the client in that case I innumerable could be used to retrieve the initial data set and then apply filter on that data set in memory if we were to implement same using iqueryable then we would have to make three trips to the database to retrieve all the three different filter condition so rather than that if you use I innumerable and filter in memory there will only be one database column to conclude if you want to apply filter in memory I queryable is preferred but if you want to apply a filter on the SQL itself and retrieve less or filtered records then I queryable should be used in most of the cases during data retrieval with filters I queryable is preferred because it applies filter on the SQL itself and as a result less number of records are returned which reduces the network traffic as well I hope you remember this next time you are confused between I queryable and I innumerable if you enjoyed this video make sure to like the video and leave a comment if you have more question or if you have other topics that you want me to cover in the next video till then Happy coding
Info
Channel: DotNetMastery
Views: 16,359
Rating: undefined out of 5
Keywords:
Id: J2u1DmnE9mU
Channel Id: undefined
Length: 4min 49sec (289 seconds)
Published: Tue Sep 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.