From the course: C#: Advanced Practices

Learn LINQ query syntax - C# Tutorial

From the course: C#: Advanced Practices

Learn LINQ query syntax

- In our last video, we did an intro to LINQ. And in this video, we're going to talk about the query LINQ syntax. So Bill, we've spoke about where and select. So what's next? - There's a whole lot more. When I'm exploring LINQ, I often like to try to find things that I don't use every day, because that's the stuff I often forget. Where should we start? - [Mika] Should we start with skip and take? - [ Bill] Skip and take is perfect. So partition operators. Let's go into that page. Which one do you want me to try first? - [Mika] Let's try the first one. - [Bill] All right. So we've got a set of numbers and it's a method called syntax. So we're going to take three. So I would figure that might do something like. - [Mika] Oh, so that returned the first set of results. - [Bill] Right. So it's got just three. Usually I think this is great for prototyping. - [Mika] All right. Let's try the next one. - [Bill] Let's try the next one? All right. - [Mika] Well done. - [Bill] So you've got a nested take. This is interesting to me. I'm seeing query syntax that from and from again and a where clause, which we talked about before, and then a select, and then it's closing. And then it's calling take dot three. - [Mika] So uses the method syntax as well. - [Bill] So it's mixing the two together. Method syntax and query syntax. So I've now got, these records are the first three orders. Does that, is that normal? Mixing them like that? Is there any penalties there? - [Mika] I dunno. Yeah. I was wondering if there is any penalty for performance. - [Bill] Actually, because take, and because that's query syntax maps directly to method calls, there's no penalty whatsoever. Where should we go next? - [Mika] Yeah, let's go to the next page. - [Bill] All right. Next page is partitions with conditions. - [Mika] Awesome. Let's do the Take While while syntax. - [Bill] Okay. So now I've got these numbers dot take while as long as N is less than six. So if I hit that, now we see we've got the first numbers less than six. So it does five, four, one, three. So this is kind of like a where, but if I look later in my sequence, there's a two and a zero and I didn't get those. - [Mika] Yeah. Why would I use that? - [Bill] Well, this one must be looking at a condition and then I'm only going to take from the beginning of the sequence until that condition is false and then I stop. - [Mika] Okay. So it can take no more than let's say 10 elements while condition is true. - [Bill] Right. So I can kind of explore data a little bit, but not get a whole lot cause I'm not sure exactly how much is there and I'm just kind of exploring a little bit. - [Mika] Cool. Yeah. Should we try something else? Let's go back to the home. - [Bill] Let's go back home. So at the bottom, we've got the link back up to home. Where should we go? - [Mika] Should we do the sequence operations? - [Bill] Sequence operations. That was down near the bottom. We're skipping around a lot. We're leaving a lot for people to do on your own. So sequence operations, right? So this first one compares two sequences for equality. So I've got a couple of different arrays and right now they match. I've got some code here it wants me to try by changing the second one just by putting a couple in changing the order of a couple of these. So now I've got, I've switched up the order between the apple and the blueberry and the cherry. And if I try it this time, now they don't match. - [Mika] All right. So they have to match pretty much. - [Bill] They have to match exactly the same elements and the same elements in the same location for our sequences to be equal. - [Mika] Great. Should we try the last one? - [Bill] Last one is combining sequences with zip. That sounds - [Bill] A little different. - [ Mika] Yeah what does zip do? - [Bill] Okay. So what zip does is it takes two different, these two different vectors, the first one, zero two, four, five, six, and in succession, matches each element with an element from the second vector. That one, three, five, seven, eight. So the input is the zero and the one, the two and the three, the four and the five, and then the five and the seven, you know, this pair, this pair here, the zero with that one, and then the two and the three and like moving through each of the sequences one at a time and building one up a sequence from that and then it sums all those values together. So where would I use something like this? - [Mika] Maybe an IOT application where you can compare data and take in on successive days. - [Bill] So I get data from Monday, I get data from Tuesday, and I can look at things at the same time on those two successive days. That would be cool. - [Mika] Great. Should we try one more? - [Bill] Let's try one more. - [Bill] That's where I go. - [Mika] All right. Let's go back to the home, and let's try combine multiple input sequences under the projection operators. - [Bill] Back under select, and we're going to go to combine multiple input sequences. So this looks, this is new. I've got two from clauses in this one. I've got from something in numbers A, from something in numbers B, and we've got a where, and I'm doing a select. - [Mika] What do they do? - [Bill] And what this thing says it does is it is going to find every pair where the first one is less than the second one. So I've got a number of elements in here, and it looks like what it's doing is it's comparing everything in the first sequence to everything in the second. Let's make one change up here. Let's just remove this where clause and kind of see exactly what those two from clauses do. So instead of filtering it, we're going to get everything. And if I do it that way now, I kind of like this. I'm seeing this zero. And then, which is the first element numbers A, paired with that one, three, five, seven, and eight in sequence from, you know, everything in sequence B and then the next one in A and the next one in B. So it kind of builds this big cross section of all the possible X Y pairs between those two sequences. - [Mika] Right. So because the first aray has seven elements, the second array has five, the output sequence would have 35 elements before filtering and using the where clause. - [Bill] Right. So we put the where clause back in and then we get only those pairs where that condition of the first number being smaller than the second one is true. - Awesome. So this is a great resource to learn more about LINQ. - It is. There's a whole lot of different things. You can show. We looked at some of the syntax here, but there's a lot more. And in the next video, we're going to show you some things about how LINQ queries execute in terms of how they produce their results and how you can plug different pieces of LINQ together to build more complicated queries. We'll see you then.

Contents