This course was created by Microsoft.NET. We are pleased to host this training in our library.
Skill Level Advanced
- Hello, I'm Bill. - And my name is Mika. - Thanks for watching our previous series, with Scott and Kendra tutorial on object-oriented programming in C#. This series is all about LINQ. So what is LINQ? - Yeah, so LINQ, it's in the name. It stands for Language Integrated Query, and the cool thing about LINQ is once you know LINQ, you can use it for any data structure. So you can use it for XML documentation, arrays, SQL databases, it's pretty sweet. - Collections, whatever? - Exactly. - Can use that for Microsoft. Others can use it, right? - Yes cool. - All right. So let's look at this using try.net, which is a tool that we built that helps you explore different libraries in LINQ. So in the description, you're going to see a LINQ to our dotnet/try-samples repo, which is the one I'm showing here. And in the read me, there is the installation instructions to go ahead and get started with the Dynacore SDK and something called dotnet tri, which is a dotnet global tool. And all the instructions are right here - [Mika] Right, so first you need dotnet core SDK. And then once you have that, you can go ahead and clone the dotnet try samples off this get hub repository. - [Bill] And I've already done that. So we're going to start with the 101 LINQ samples and then you just have to run a tool called dotnet which you learned about in the earlier series and then do.net try. And that's going to start up a new browser window for me with all of these wonderful LINQ samples. And once it loads up the browser we're going to get a new local host window on our space. And there we have 101 LINQ samples and our friendly dotnet bot and we're not going to run all these, these are crazy. - [Mika] Yeah, I mean, LINQ is powerful. So let's start with one of the most common queries click the restriction operators LINQ. - [Bill] All right, so restriction operators, the way our keyword. So I get this window and I've got a run button. So now when I click run I get this other window that shows, here's the results from our query. So this query, if we look at, it's looking for lower numbers from numb and numbers, like you said just like SQL in some ways where numb is less than five that's kind of that restriction operator for where and then we select the number. - [Mika] Yes, so let's change that to a list. - [Bill] Right, so we've, we said we could do this with anything. So I'm going to change those numbers to a new list event. I'd have to change it on this side. That was one of the things we learned about earlier in our object oriented. That's the only change you have to make. - [Mika] Yeah, so you don't have to change the Query at all. It just worked, pretty much. - [Bill] And then we'll just run it again. And once again, there we go. All right, so let's look at a couple others here. - [Mika] So let's look at the properties of an object and the input sequence, so-- - [Bill] Okay, so if I take this very next one, we've got a list of products. So that's some object cornea collection that we're building. And I'm looking at with that, where clause here on anything where there's nothing in stock, right? So that should print everything that we've sold out. Those are the popular ones. And there, it's just looking at the same sequence and it's printing everything about that particular product. So what else can we do with this where method? - [Mika] Let's look at the last page, which shows the syntax for a where clause. - [Bill] Okay, this one does look different. - [Mika] Yeah, it's using a where a method instead of the, where keyword. - [Bill] That's right, so this one uses the where method. It actually does the same thing that we saw in these other ones that we were doing, with this where clause and a keyword, what's different is the language compiler says "Translates one into the other." And there's a slightly different feature here. If you look this where clause or this, where method we've got two different arguments. One is that index that says it's its position of that element in the sequence. So if we run this one what we're going to see is an addition to the strings up above that zero, one, two, three with the letters it also knows the corresponding number, the number zero and the number one. And it's printing out any words that are shorter than the value. So like the number six printed out has fewer letters than six and so on and so forth. And that's everything with the word method. - [Mika] Great, so let's look at that projections operator LINQ right there. - [Bill] Right there? All right, now we're on the select. We kind of saw some of these. But I'm going to just do one quick. So if we look, I've got one here where we're taking things and numbers and a mapping from one to another. So our first select just grabbed everything from our input sequence. And here you can see I'm taking something from a numbers collection. And what I'm selecting is the corresponding string from another array. So select lets us project into some other collection. And that's what we see in a lot of quick, just find every query we'll see some kind of where to filter something and some kind of a select to have some upward sequence. - So that was an intro to LINQ, and we also showed you how to use our tri.net tool. And we also showed you all of the 101 LINQ examples. And next, we're going to talk about the LINQ query syntax. - See you then.