LINQ in C#.Net made easy! - PART 2 | OrderBy | ThenBy | GroupBy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi friends welcome to another video of code Allegiant this is a continuation of the link queue series which we are having so if you are new here do check out the part 1 of this where we have explained the basic link you and some basic operators which we are using any link you so from here onwards will it will be a continuation for the part 1 so now let's see what is an order by operator auto my operator is used for ordering the data based on some property so what exactly it is for example if there is books which is a collection of book objects so we can say dot order by and using lambda expression we can say B operator lambda operator B dot price so the nothing but we are telling that lit this list should be ordered based on B dot price so the output of this will be again a new list and which will be having the book object itself but it will be ordered based on price so again here we are using lambda operators so if you are not comfortable with lambda operator or you are not aware of lambda operator do check out our other video related to lambda expressions and there is another method which is provider that is order by descending so this order by by default orders in the ascending order of price whereas if you want it in a descending order you can use order by descending and the same property so the same thing we can write it in query base the syntax this way wherein we say from B in books order by B dot price and then a select what you want to select from this list so again if we are selecting B that means you will be returning the book object itself so as I have said previously in the part 1 also that any statement which you are writing in query based syntax should end with select or group so you cannot end after the order by whereas in the method based syntax it is allowed so again here if you want the ordering to be in descending then you have to use order by B dot price and the keyword descending so again if you might have requirement like you want to order it again and again like being a first-time you want to order it maybe with the price but again if the price is same for two books in what order it should arrange that so for that there is an option of having multiple order buys wherein the first order buy whatever the output the the first order by gives you can apply the second order by on top of that so but in method based syntax you cannot use again one more order by instead there is a method called then by so you will have to use books dot order by B dot price so it will first order it based on the price so if at all there are two three books which are of same price and you want to order them based on the available quantity then you have to extend this by saying dot then by B dot quantity so first util order by based on price and if the prices are same out of that set it will be ordering it based on the quantity which you which are available so the same thing you can write it in query based syntax much easier like there is no than by keyword or anything here you just have to say from B in books order by B dot price comma whatever is the second property you want to order it so you can say B dot price comma quantity and if at all again after this the quantity also is same for the sunset then you can say comma B dot name you want to order it on the name whatever ways so in this query based syntax you can just put commas and keep putting your properties in which order you want to order it whereas in the method based syntax you have to first use order by and then continuously you can use then by so let's see in code how it works so again I'm in visual studio and this is the control application so as I've shown in the part one I am using the same book database and the set of books which I have already created so here we have a collection of books so now I will define where a one variable called data and I want to order this based on the price so I will say books dot order by and the lambda expression so B is the variable Oh operator B dot I want to order it based on price so now I will just print this and see how the output comes so for each wear item in data and I will have a console dot write line so maybe I can print both the name and the price so that you can see it is getting ordered so here it is item dot name and I will print the price as well so I am dot price so now when we run this you can see it's ordered in the price 180 250 350 600 800 800 and 1100 so this is now ordered based on price and if you want to write the same thing in the query based syntax so that will be where data equal to from B in books order by B dot again that will be the property that is B dot price then select and what you want to select so I will be selecting the whole book object so I can say select B and the print statement remains the same so now you run it it is again in the same order but here you will notice like there are two books which are having the same price so 800 800 so you can decide in these kind of sets what should be the next criteria for ordering so maybe I want to order it based on the name so here if you see till this character it is same and here it is s and edge so ideally when we are doing ascending order the H should come up and the S should come next so maybe that will be our second condition so in the query based syntax you just have to put comma and you can mention the second property on based on which you want to order it so I will say B dot name so in this situation first it will order it based on price and whatever output is returned that is again a list in that if at all any two data is having the same price then it will apply a ordering based on the let's see then output of this so now you can see the two values two books which are having same price is ordered based on the alphabets which are there in the names so if you want to do the same thing using method based syntax so that will be like we are having already a order by clause after this you have to say done by so multiple order byes are not allowed so if at all you use order by a means it is not restricted you can use order by but then the first order Y will be ignored and only the second order by will come into effect so if you don't want that then you must use then by and here again you will have to use the lambda expression and you will specify which property you want to order with so now this statement is equivalent of this statement and let's see the output again the output will be same so these two books are having the same price and it is ordered so anytime if you want to change the order maybe you can have order by descending here or if it is a query based syntax after this order by you can just specify descending keyword so then the whole order will be in descending order so for example now you can see the costliest book is on the top and then it goes down but again the H and s is in that order because for our then by clause we have not used then by descending so it is still in the ascending order so this is how you use order by Clause now let's try to understand what exactly is a group by operator goodbye operator is used for grouping the data you might have multiple criterias based on which you can group your data for example if we have a list of books maybe we can have multiple books by the same author and you have a requirement wherein you want to group them based on the author so then you can use books dot group by and inside that the lambda expression B is a variable lambda operator VDOT author so now this books list will be grouped based on author but in this unlike other operators which we have seen till now the output will not be that simple the output will be again a list but in that list you will have a different set of data which is grouped it will be a key value kind of data which you get output and the key will be the property which we have specified here to group it and the value will be the books object matching with that particular keyword so we will see the detail in the code meanwhile first you will see how the same thing can be written in the query based syntax here we will say from B in books group B that is the book we have to specify what you want to group and then by keyword and the property in using which you want to group it so the syntax is slightly different from the method by syntax so this you need to make sure like because it is slightly different from other operators where in the group by is the method which you use in method by syntax whereas in the query based syntax it is split into two parts so group what you want to group and by using which property you want to group so let's see an example in the code so here I have the books collection already so what I will do is I will define one variable where data and I want to group them based on author so I'll say books dot group by and here I will specify B B dot author so here when you see the output the output is of ienumerable I grouping off string comma book so the string is nothing but the key that is the author name and again you will see it is a book object material which is there so again when you use this for printing like for example if I show you as I will print try to paint this so we'll be data and here I will have console dot write line so if I say item dot now you can see there is a key and then there will be a set of books loss associated with this key so if you want to just print this key now if you run this you will see it has been grouped based on the author but again it's difficult now to print the data which is there because again that will be a collection internally so for such situations there is a feature provided in group by which is nothing but group data can be created into a different list based on whatever property you want to extract from the group data so how you do it is books dot group by and the property based on which you want to group so this much remains the same after that you can specify a select statement so you can say dot select and this will be a new variable so this variable will contain the group data already which is a group and there you can say new and you are creating a new type of this we have seen already in the select operator when we try to the select operator and there you can specify your own type where like author which is nothing but group dot key so this data which is grouped will have a key value pair so key is nothing but by is specified by the property key and next I want to just group the data which is grouped based on author and I want to see how many counts of books are there under each author so I want to say book count and I can say G dot count so again count is an extension method which is provided by link you and the same thing you can achieve in query based syntax by saying the same from B in books Group B by B dot author into keyword and the variable name which you want to use so here you have defined the variable inside the select operator here you will have to specify into keyword and specify the variable now you will say select new and this variable which you have declared will be available into this Senate now you will say are equal to G dot key and book count equal to G dot count so this remains the same so only difference here comes is the into keyword which is introduced for the group by so now let's see how we can do it here in the code so this author I want to do a select and in the Select I will save some variable give a G and I will create a new type and in this first thing I want to create a property called author and if you say G dot you can see there is a property called key available so this is nothing but the key based on which this group is available and next I want to see the book count so that you will say the group dot count so this will fetch you the count which is available in each group so now when you are looping through this I want to print the key and the count so now since the key is not available because the key will be available only if you return from here now you have changed that and you are returning a list which contains author and book count so now if you see here the moment you say item dot you will see author and book count available so I'll say author and I will print the count as well item not book count and now when you run this you can see it is grouped based on the author and you can see the count of books under each author so again this is in different order like 2 1 2 1 if you want to order it here you can just append it with order by and again you can use your order by clause so there is no restriction of mixing these kind of operators so we will say B dot now you have the book count here and you'll just order it based on the count so now you see it is order based the counts which are available in the groups so I'll remove this order for naught and we will see how we can do this using the query based syntax so we will say where data is equal to from B in books so till here it remains same then we will say group B by which property so we'll say B dot author then we will say assign this into a new variable so that is maybe G or whatever name you want to give say maybe G B and on this you want to do a select and in the Select you want to create a new type and the new type will have two property one is maybe the author that is nothing but our G B dot key and another one is the book count so here you can say G B dot count so that's it so now this returns you the same output which you got from here so when you run it you get the same output so this is how you use a group by clause so you can play with this in multiple ways like you can group it based on the names like for example if at all you don't want the count you want to extract the book names I'll just comment this for now and let's see the method based way so instead of having D G dot count you want to have the books so I will create books and here I will say G dot select and in the Select again I will have to say some variable maybe a book and here you can say book dot now you can see all the individual books properties so in this group you had a key which is nothing but author for each key you had multiple books so when you say dot select you are now looping to the list of those books so now you can say the book name and that's it so now you have created a new list which is having a key value pair the key is nothing but author and the value is nothing but again a list of book names so if you want to print this NC now I will say I will have to loop it multiple times so now first we will print the author maybe here and then again we will have to loop in the where book in item dot books so since now books is also an ienumerable type that is internally it is a list type so now we will have to loop through this and here i will do a console dot write line and maybe just to make a small difference i will give two spaces so that it is under one author and here I can say book so book is again a string time that is nothing but book name so now this if you print you will get the group data so you can see one authored two books under it and another author 1 book under him then again one more other and two books under him so this is how you can group the data and if you don't want only the book name you want the complete book then you can just say the G dot select here and say return the book itself so now if you see here you will get the complete book object itself so if you want to print here book dot maybe name and if you want to print the price as well so you can say book dot price so now this when you run it now you can see the author the book name and the price so this is how you can play with the group by so until now you have seen the where class order by clause select class group by clause so if you want to mix them all together that is also possible so for example I change this code maybe I will remove this code first so that it is clear and I will try to mix all the data like all the classes so first I will say books dot where maybe I will first list down all the cheap books so maybe I will have where B B dot price which is less than maybe thousand rupees so as you remember like there is one book which is more than 1100 so that we will be removing and out of this list what we get we will say group buy and here what we will group by is B dot author so based on group we will based on author will group it now we will say select and here we will say grouped item and here we will create a new type which will have again the same which we did like author and the third will have the value from the key and maybe the list of all the books or maybe only the names we can take it so like books equal to GB dot select and from each book will extract only the book dot name and whatever output we get on that we will say order by so here we will order it may be some other variable so I'll say T dot I want to order it based on author name so now when you sprint this maybe I will change this code here slightly so because since we don't have the book name and price we have only the book name and now when you run this now you can see one author is removed on top of that you can see it is already grouped and on top of that you can see the name of the author is in an ascending order so this is how you mix the multiple classes like we have mixed where clause group by clause select class order by clause so this is how you can mix it so the same thing you can achieve it in query based syntax as well and finally these are few useful operators which will be helpful when you are dealing with linked you so first one is single so this method will look for one item matching the condition which you pass in these parameters and if at all it is not found or it is found more than one matching then it will throw an exception and there is another method called single or default so it works same like single the only difference is it doesn't throw any exception when it is not found or it former finds more than that and if at all it has not found it will just return the default value like that we have first first or default which looks for the first item matching further condition and same like that we have last last or default which looks for a last item matching the same condition and we have some aggregate operators like min Max average sum and count so min looks for the minimum of the list if it is a numeric list it will just take out the minimum of that or if it is a list of objects and you are the parameters you have to specify the predicate that is nothing but your lambda expression to tell based on which property you want to get the minimum and similarly the max which gets a maximum average gets a average of the all the list sum gets the list of some of all the numeric elements or the property which you mention and finally the count which gives you that list count actually if you specify the numeric if it is a numeric elements list it will directly give the count or you have to specify the condition so in the condition you will have to specify a predicate that is nothing a lambda expression which returns a boolean type so this we have already seen in one of the examples wherein we were getting the count of books so I hope the operators are clear so based on these operators we will look into more details where and we can play with lot of joins and all but you should be thorough with these operators to get into that thank you
Info
Channel: CODELLIGENT
Views: 1,006
Rating: undefined out of 5
Keywords:
Id: Ftn9zYygBDw
Channel Id: undefined
Length: 24min 38sec (1478 seconds)
Published: Wed Mar 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.