C# Tutorial 15 LINQ Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we'll alternate and welcome to part 15 of my c-sharp video tutorial in this part of the tutorial we're going to focus in completely on Linc now I have covered Linc methods in the past but this time I'm going to cover Linc quarries and more specifically I'm going to cover from where order by select enumerables inner joins as well as group joins now those are pretty vast topics and I'm going to just provide a whole bunch of examples of how to use them and like always all the code as well as a transcript of this video is available in the description underneath this video and I have a lot to do so let's get into it okay so I went in did a little bit ahead of time I created an animal class there's your beloved animal class everybody likes and it just has a name weight height and animal ID property inside of it and you could pause the screen or like I said get the code below and then just copy and paste it and here is a constructor for it and here is the to string method and all it is going to do is take the different properties available in this animal class and allow me to print them out like a string okay so pretty simple stuff didn't want to waste time on that so I could focus on wink also I created an owner class and it just has a string name property and an owner ID property so that is all that I created ahead of time and now I'm going to jump over into program and just start writing the lock code okay so here I am there's the top of the code right there and here is the main function now what I am going to do is I am going to create a function I mess around with arrays I'm going to query arrays collections a whole bunch of different things so I'm going to create a string array or I'm going to query a string array here and then I'm going to have the main function up here call it to execute it so we'll go query string all right right like that alright so now we're down inside here so we're going to need some data that we're going to be messing around with and there it is it's a string array that contains a whole bunch of different dog names and we're going to perform some queries on it to manipulate to this information once again I make videos that are meant for pausing so pause your screen typing in or the code copy and paste it alright so what I want to do here with this query is I want to get strings with spaces inside of them and then put them in alphabetical order so a pretty simple way of messing around with this array so we're going to create this a little query and I'm just going to call this dog spaces and from is a link command and it is just designating where this information is going to be coming from so it's going to be coming from the dogs array and the it's going to be stored temporarily in a dog variable and then where it's going to define conditions that must be met to succeed to be able to be put into my new collection that I create here so what I'm going to say is where a dog is going to contain a space because I said that I wanted to only get dog names that contain a space and get rid of that dot right there all right then we're going to use order by to define what data is used and for in in regards to how we're going to order the data so I'm going to call this order by I'm using dog of course and you can either put ascending inside of here right there so just to do something different I'm going to say descending and then finally select is going to define the variable that is checked against this condition and is going to be dog of course all right and there is a query we're going to do more and more of these and you will completely understand them basically link which stands for language integrated query is going to provide many tools for working with data now link is similar to SQL but it can work with data asides from databases and just like SQL you're going to manipulate this data using what are called curry expressions ok so I got my information here so what I want to do is just cycle through it so I'm going to say for each bar I in dog spaces I know that the horrible name but it's just what's coming into my head as I'm typing this and then I'm going to use the CW a little trick to print that out there real quick and I'm going to print out that information and after this I'm just going to do a new line alright so now we can run this and see how that query works and you can see right there it went in and only grabbed dog names that have spaces inside of them and then it put them in reverse alphabetical order so let's go in here and do another one so I'm going to create a what I'm going to cycle through this time is an integer array so I'm going to create another method inside of here I'm going to call this static it's going to return an integer array and I'm going to call it query int array it's a pretty simple stuff just want to show you how to perform queries on different types of data and I'm going to come up here and I'm going to call the or I'm going to call for it to execute query integer array right like that okay so let's come back down inside of here and let's play around with this guy so I have to create an integer array and I'm just going to call this num and I'm going to throw some data inside of it so I'll say some point 5 10 15 20 25 30 35 just with a coven alright so there's an integer array that I'm going to be pouring so let's say I want to get numbers that are bigger than 20 so I'm going to say let's call this GT 20 is equal to and again I'm going to go from where I'm getting the information from like that and then with where I'm going to define my conditions so where num is greater than 20 and then I'm going to say order by num and then I am going to say that I want to select num that's what's going to be stored okay that's pretty simple and then we can just come in here and we can cycle through all this information just to show that it indeed worked so in GT 20 and then let's come in here and let's just print that information out so pretty simple and let's throw another break here at the end of it alright and let's run it whoops I have an error what's my error oh it doesn't return a value I forgot to do that nuts I've also wanted to demonstrate how to return a value so you know what I'm going to do I'm going to come in here and do a couple more different things so let's say that I so let's say I also want to come in here and find out what the data type is this guy I can do that let's come in here and let's say get pipe and go Gigi 20 and of course we would go get type all right so some additional information and don't forget to put those little parentheses inside of there let's get rid of this extra thing right there all right so that will get me the datatype for this and why don't I also show you how to convert it into a list we can go VAR and list greater than 20 is equal to GT 20 to list and this guy is going to be an integer list and that's how we can do that and also let's come in and GT 20 and convert this into an array so we can go GT 20 and to array and that's probably important to cover because you very often we're going to want to do things like convert them into lists and convert them into arrays one thing is kind of interesting is if you would see up here we called for this to print out what I'm going to do up here is come in here and change the data so we issued the query above that the neat thing is after you change the data the query is going to update and you're going to see what that looks like so even though the information you don't have to issue the query over and over again it's going to be something that automatically updates so let's come in here and let's do another for each for I in GT 20 just little crazy things come into my head hey why don't I cover this also all right so we got that and we can output this and then let's go and do another break and then finally let's return the the integer array that we promised we would return so I'm going to go array TT 20 and return it all right so no errors and let's run it see how that works out you can see right here that this comes back as an ordered enumerable see there we print the dog names here we got 25 30 35 and you can see here also that we went and updated one of the values inside of our array and we didn't have to issue the query again to have information updated so pretty cool stuff so what I want to do now is work with array lists so I want to create an array list so I'm going to call this guy static void once again query array list seems to be a logical name forming here throw that in there and we're going to have to create our array list again just to save a little bit of time I'm going to create an array list I'm going to call it famous animals ArrayList and I'm going to throw in some animals and then I have the height and the weight of course and I'm not going to mess around with ID right now you see Heidi and Shrek and how big Shrek is and the weight and then Congo and once again 3.8 and the weight is 90 all right so got all that information in there so now we're going to mess around with it and query it now one thing that's important is you're going to have to convert the ArrayList into an enumerable and to do so I'm going to come in and I'm going to go famous animal enumerable and to do that we go animals famous animals and I'm going to say of type and state that they are the animal type and then I'm going to issue my query on them so I'm going to go I'm just going to SM animals is meaning small animals as what I'm doing here and what I want to do is I want to look for animals that have a weight that is less than or equal to 90 so once again animal what I'm going to be pulling this information from the famous animal innumerable and once again I'm going to say where animal weights is less than or equal to nine and you could put additional conditions inside of here with ands or or whatever you want but I'm just going to keep it simple die right now once again I'm going to say order by here I'll say animal name and then finally select animal now that I have all that set up we can cycle through here I'll go animal in small animals and then I can well let's go and print this out go like that and then we can go get the animals name out of there and we could say ways and get the wait for our animal and that will be pounds just to keep that a little bit simple and then to get the specific information I'll just go animal name and then animal and wait alright and did I call for this to execute inside of my main function don't know if I did or not let's like go up here and doesn't look like it alright so I got that in there don't see any error so let's run it and see how it works and there you can see we only got animals that are 90 pounds or equal to 90 pounds or less I guess it's a say and it went and printed out the name as well as the animals weight it's a pretty useful stuff here and now what I want to do is show you how to work with a collection so let's come in here get rid of that and for this guy let's throw another break inside of there and let's create a brand new method inside your static function all right so we'll go static void and this time a moment to be pouring a collection just to show you another thing that you can query and I went and throw a collection inside of there just to save some time you can pause your screen type that out if you'd like to and then I'm going to move on okay so now what I'm going to do is look for big dogs that have a weight that is greater than 70 and have a height that is greater than 25 all right so we're going to go big dogs is equal to once again just going to define where the information is coming from it is coming from my animal list that I just imported there and I'm going to then define my conditions so I'm going to say that I want dog that's weight is greater than 70 and like I said you can put and inside of there whatever you want and I'm going to say dogs height is greater than 25 all right so now what I'm going to do is go order by and I'm gonna order by the dog's name and then once again we can just go in here and go select dog you're going to see other ways of using select here in a little bit so pretty cool stuff and we can output all that of course so we can go dog the big dogs and then let's output that information so we can say a and throw that there this is going to be a dog breed ways and put that there pounds and let's just keep it at that and then we're going to output you know get the actual information so we'll go dog name whoops dog name like that and dog and weight and let's throw a new line in there after that just to keep everybody happy and let's go and run it and now you're going to say it went in there whoops did something wrong oh I know it it did I'd forgot to call this function from up side main all right so let's just grab this guy right here zoom right up to the top and let's go and call that function to execute now let's run it and you could say that the st. Bernard is the only one that comes back as qualifying for our conditions and it prints out a st. Bernard weighs 200 pounds okay so pretty cool stuff okay so now what I want to do is I want to show you how you can come in here and do some more things with select like I promised you could do as well as how to do an inner join and a group join which is very very useful so we'll go like this and we'll say query animal data is the name of this and let's just go and call laughs don't worry about it all right so I'm going to throw in some information here okay so I went and created an animal array called animals equals new and I went and created individual animals inside of it so I've German Shepherd height weights and animal ID animal ID is going to be used to perform my different joins so that I can join this information that I have right here on the screen with the information that I have down here which is going to be owners so you can see there's the matching IDs and how they are going to match up with the different animals that we have up here all right so let's go and another thing that be kind of cool is you can actually go and remove just let's say we want to just remove the name the height and how you can do that is with select so we can come in here and do something like name height is equal to and we can go from a in animals and then after that just go select new and we're going to create a brand new guy inside of here and it's going to be made up with the name as well as the height so there we are we just created that guys and that's just something kind of interesting you can do is select all right so you're just pulling those two things out and making a brand new collection you could then come in here and convert this to an animal rack and how you would do that is go array name and height is equal to and go name height like that and then go to array so that's going to convert it into an object array like I said before and we can also then come in here and output this information so we'll go for each or I in and this is going to be array name height and then we can call for this information to print out on the screen so we could just go I and let's just keep it simple and just go eye to string so there that is and let's throw in a break there as well and if we run that you're going to see whoops did I do something wrong oh I know what I did I forgot to call this function again all right so let's come up here and let's get Cory animal data zoom way up to here to this guy and right there throw that inside of there and that like that and let's run it and now you can see that it returns only the name as well as only light all right so neat stuff let's do some more now what I want to do is create what is called and inner join and what this is going to do is join info and the owners and animals using equal values that exist in the IDS and what I want to do specifically is store values for the animal and the owner specifically so I'm just going to call this inner join and then we'll issue our core a so I'm going to say from animal in and um moles and then I'm going to join and I'm going to join owner in owners that is this guy up here this is what I'm going to be messing with these owners array up here and of course this animals array right there so those are the two guys I'm going to be playing with so I'm going to say joint owner in owners on animal and then specifically animal ID jump down to the next line that's perfectly fine equals owner owner ID so these are going to be joined based off of equality between the IDS of the animals as well as the owners and then do something fancy with select I'm going to go select anew and I'm going to say owner name is equal to owner name obviously and let's jump down to the next line and then say animal name is equal to animal name alright so there we got a fancier type of query that we just issued now what we're going to be able to do is cycle through this information so I'm going to say var I and n inner join and then let's output the information so we will say go like this and owns and then the animals name after it like that and then to output that information we'll just go i dot owner name and i dot animal name and those come from these guys see these were variables that were created inside of here that with we are then going to be able to access all right and we're going to be able to access them by referencing the inner join that is where that information is going to be created and then throw in a break and then let's run that again and here you can see comes back doug parks owns german shepherd Sally Smith owns to Iowa da-da-da-da-da-da I guess I should say owns a but whatever you get the point of it so now let's do something even cooler we're going to do a group joint and what a group join is going to do in this situation is get all the animals and put them in a newly created owner group if their IDs match the owners and what we're going to do is print out the owner's name followed by the specific animals that that owner owns so I'm going to go VAR group join is what I'm going to call this is equal to and I have to define where the information is coming from so I'm going to say owner in owners which is the owners array and then I'm going to say I want to order by owner thought owner ID so these are going to be ordered by the owners ID then we're going to do a join or define how this information will be joined so I'm going to say join animal in animals which is the animals array and we can down in the next one on owner dot owner ID and then go down to the next slide the situation in which they are equal to the animal the animal ID and then I'm going to say that I want them to go into a new one that is going to be called owner group after that I'm going to go select new and then inside of here I'm going to define what I want to deal with so I'm going to go owner is going to have get the value for owner name and then animals and you can throw another query inside of here we can go from owner to this is just at the owner to is just a temporary holding cell just like animal is an owner is up there okay so that's going to hold the value in we can get out of next one in owner group this brand new group that we just created right here that's what I'm referencing order by owner to name the bounced way over there whatever that's what happens whenever you're messing around I have my font size extremely big so that's the reason why that happened and then I'm going to say select owner to at the end of that come down here and throw a semicolon in there all right so that's probably about one of the more complicated queries you're ever going to see all right but what it does is going to be really really cool so what I want to do is I'm going to go total animals and then I'm going to quarry through all this information so I'm going to say for each bar owner group this brand new group is what I'm going to be working with group going is where I'm getting this information from and then I can output this information so I'm going to say owner group owner and that's going to give me the owner's name and then I'm going to cycle through the owner group animal list and print out each animal underneath of each owners names so megha animal in owner group animals let's go do another one inside of here and I can cycle or I can increment the values for total animals if I'd like and then I'm going to come in here and output some information so let's say I want to do like a bullet point and do a bullet point like that like this and close that off and then underneath of it go animal and get the animal name all right use of unsigned local variable total animals what I do you know what I don't even think I need this I was trying to do something fancy but I don't even need it so let's just get rid of it all right so let's save that and let's run it and there you can see it went and printed the owner's name and followed by the specific animals that that owner owns alright so there's some pretty cool stuff you can do with a link like I say always go in there experiment play around with your own data and just try things out break things learn things do things that's how you learn so like always please leave your questions and comments below the last till next time
Info
Channel: Derek Banas
Views: 154,293
Rating: undefined out of 5
Keywords: LINQ, C# LINQ, LINQ Tutorial, Learn LINQ, LINQ Query
Id: gwD9awr3NNo
Channel Id: undefined
Length: 23min 41sec (1421 seconds)
Published: Sat Feb 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.