1 00:00:00,540 --> 00:00:02,230 Here we are in workspaces. 2 00:00:02,230 --> 00:00:05,640 You can follow along by opening up workspaces by clicking the button below. 3 00:00:06,830 --> 00:00:10,710 We're going to use the C# REPL and the console of our workspace here. 4 00:00:10,710 --> 00:00:14,510 To launch, we will enter csharp and press Enter. 5 00:00:16,390 --> 00:00:20,584 The first thing we need to do is make sure we've got the system.link namespace. 6 00:00:20,584 --> 00:00:27,124 In the C# REPL we can just enter, using system.link. 7 00:00:30,464 --> 00:00:32,844 It says we've already got a directive for it. 8 00:00:32,844 --> 00:00:37,833 That's because it includes the using directives for system, system.link, 9 00:00:37,833 --> 00:00:42,384 system.collections and system.collections.generic by default. 10 00:00:42,384 --> 00:00:45,485 Let's start out with a list of integers. 11 00:00:45,485 --> 00:00:50,751 List numbers = 12 00:00:50,751 --> 00:00:56,016 new List { 2, 13 00:00:56,016 --> 00:00:59,699 4, 8, 16, 14 00:00:59,699 --> 00:01:03,924 32, 64 };. 15 00:01:03,924 --> 00:01:09,164 If you haven't seen a list before, a list is a type of generic collection in C#. 16 00:01:09,164 --> 00:01:13,368 When a type is generic, it means that it's designed to handle any type. 17 00:01:13,368 --> 00:01:17,086 So we can declare a list and put whatever we want into it. 18 00:01:17,086 --> 00:01:20,680 But we have to tell it what to expect when we declare it. 19 00:01:20,680 --> 00:01:23,920 We've passed int as the type parameter to the list. 20 00:01:25,120 --> 00:01:29,450 Then we're instantiating a new list and initializing it with some integers. 21 00:01:30,870 --> 00:01:34,090 If you haven't seen this before, it's similar to initializing an array. 22 00:01:35,230 --> 00:01:39,500 We don't have to do it that way but it's a nice short cut to save us some time. 23 00:01:39,500 --> 00:01:42,230 We'll get into some more object initialization later on. 24 00:01:43,270 --> 00:01:48,030 It's important to note that the list collection implements I innumerable of T, 25 00:01:48,030 --> 00:01:49,440 which means we can use Linc with it. 26 00:01:51,220 --> 00:01:54,611 Say we need to only get the integers out of our numbers list that are greater 27 00:01:54,611 --> 00:01:55,166 than 10. 28 00:01:55,166 --> 00:01:56,846 How would we go about that? 29 00:01:56,846 --> 00:01:59,818 Well, we would write a for or a for each loop. 30 00:01:59,818 --> 00:02:03,310 First we need to create a new list to store our result. 31 00:02:03,310 --> 00:02:07,340 Then we'd write the for each loop that would iterate through our numbers list. 32 00:02:07,340 --> 00:02:08,460 Inside the loop, 33 00:02:08,460 --> 00:02:12,130 we'd write an expression that would check if the current number fits our criteria. 34 00:02:12,130 --> 00:02:15,520 And if it does we'd add it to the new list. 35 00:02:15,520 --> 00:02:19,398 Once loop is done, we've only got numbers that are greater than 10 in our new list. 36 00:02:19,398 --> 00:02:20,386 Let's see how we would do that. 37 00:02:22,464 --> 00:02:24,548 Declare a new list. 38 00:02:24,548 --> 00:02:29,263 numbersGreaterThanTen = 39 00:02:29,263 --> 00:02:33,778 new List();. 40 00:02:33,778 --> 00:02:39,061 Then our for each loop foreach(int number in 41 00:02:39,061 --> 00:02:47,370 numbers). our expression. 42 00:02:47,370 --> 00:02:52,519 If the number is indeed 43 00:02:52,519 --> 00:02:57,126 greater than 10, 44 00:02:57,126 --> 00:03:03,088 then numbers greater than 45 00:03:03,088 --> 00:03:08,516 ten.Add(number). 46 00:03:08,516 --> 00:03:13,836 And then our number's greater than ten. 47 00:03:13,836 --> 00:03:17,916 Should only have 16, 32 and 64. 48 00:03:17,916 --> 00:03:23,516 Now let's do the same thing using a link query expression. 49 00:03:23,516 --> 00:03:29,111 from number in numbers where 50 00:03:29,111 --> 00:03:34,239 the number is greater than 51 00:03:34,239 --> 00:03:39,376 10, select number;. 52 00:03:39,376 --> 00:03:43,956 And that also returned 16, 32 and 64. 53 00:03:43,956 --> 00:03:47,079 We just wrote a link query that did the same thing as our for 54 00:03:47,079 --> 00:03:50,150 each loop but was way faster to write and easier to read. 55 00:03:51,280 --> 00:03:53,320 And our original numbers list wasn't modified. 56 00:03:55,900 --> 00:03:57,910 Our query just returned a new collection. 57 00:03:59,300 --> 00:04:02,140 Writing that query may feel a little different than what you might 58 00:04:02,140 --> 00:04:04,100 be used to in C#. 59 00:04:04,100 --> 00:04:07,450 If you've worked with SQL, the clauses FROM, WHERE and 60 00:04:07,450 --> 00:04:09,690 SELECT might be familiar to you, but 61 00:04:09,690 --> 00:04:12,690 the words have moved around a little bit from how you'd write them in SQL. 62 00:04:14,190 --> 00:04:16,730 Let's talk about how we wrote that statement a little more. 63 00:04:18,080 --> 00:04:23,710 A link query is composed of a range variable, a source, a query, and a result. 64 00:04:25,130 --> 00:04:30,864 Looking at our query expression we just wrote, the range variable is this part, 65 00:04:30,864 --> 00:04:33,210 from number. 66 00:04:33,210 --> 00:04:37,410 Then we have our source, and numbers, and then our query, 67 00:04:37,410 --> 00:04:42,210 where number > 10, and finally our result, select number. 68 00:04:43,340 --> 00:04:46,220 The range variable part might be a little confusing. 69 00:04:46,220 --> 00:04:48,080 Why did we have to do that? 70 00:04:48,080 --> 00:04:51,680 Its job is to be a reference inside the query. 71 00:04:51,680 --> 00:04:54,630 Take a look at our previous example with the for each loop. 72 00:04:54,630 --> 00:04:57,150 We have to declare an int number, so 73 00:04:57,150 --> 00:05:00,690 we could refer to it inside the for each loop. 74 00:05:00,690 --> 00:05:05,750 That's exactly what a range variable is doing from number. 75 00:05:05,750 --> 00:05:10,056 In fact, I could call it anything I want, from xyz. 76 00:05:10,056 --> 00:05:13,476 I named it number though to be more descriptive. 77 00:05:13,476 --> 00:05:17,900 Some developers prefer to use a single letter as the range variable name. 78 00:05:17,900 --> 00:05:20,740 And it's generally okay to do that when you deal with variables that 79 00:05:20,740 --> 00:05:24,190 have such a small scope as with link queries. 80 00:05:24,190 --> 00:05:26,420 You'll probably see both out in the wild. 81 00:05:26,420 --> 00:05:28,770 Let's see how readable this is. 82 00:05:28,770 --> 00:05:33,022 from N in numbers 83 00:05:33,022 --> 00:05:37,578 where N is greater 84 00:05:37,578 --> 00:05:42,750 than 10 select n. 85 00:05:42,750 --> 00:05:47,220 A lot of developers like to increase readability even more by separating 86 00:05:47,220 --> 00:05:51,190 out each component of the link query onto its own line like this. 87 00:05:51,190 --> 00:05:58,630 from n in numbers where n is greater than 10. 88 00:05:58,630 --> 00:06:03,410 Select n. 89 00:06:03,410 --> 00:06:06,926 We can see that it's returning a sequence of integers, but 90 00:06:06,926 --> 00:06:09,750 is it returning a list of integers like we want? 91 00:06:09,750 --> 00:06:13,511 Let's assign the result of our query to a variable. 92 00:06:13,511 --> 00:06:21,468 List of int numbersGreaterThanTen 93 00:06:21,468 --> 00:06:26,772 Equals from N in numbers 94 00:06:26,772 --> 00:06:31,811 where n is greater than 95 00:06:31,811 --> 00:06:35,974 10, select N. 96 00:06:35,974 --> 00:06:42,576 Ha, we got an error cannot explicitly convert type System.Collections.Generic.IE 97 00:06:42,576 --> 00:06:46,726 numerable to system.collections.generic.list. 98 00:06:46,726 --> 00:06:51,244 Let's change it to IEnumerable then and see if that works. 99 00:06:51,244 --> 00:06:58,878 IEnumerable numbersGreaterThanTen = and 100 00:06:58,878 --> 00:07:05,484 one more time a feeling from numbers. 101 00:07:05,484 --> 00:07:07,344 Whoops, did that wrong. 102 00:07:07,344 --> 00:07:12,356 Let's cancel out of there, and 103 00:07:12,356 --> 00:07:19,597 IEnumerable numbersGreaterThanTen 104 00:07:19,597 --> 00:07:24,237 = from n in numbers 105 00:07:24,237 --> 00:07:28,884 Where N is greater than 10. 106 00:07:28,884 --> 00:07:33,844 Select n. 107 00:07:33,844 --> 00:07:36,996 Link queries always return an enumerable of the type that was in 108 00:07:36,996 --> 00:07:38,368 the original collection. 109 00:07:38,368 --> 00:07:42,398 This is important because it means that the link query isn't evaluated until 110 00:07:42,398 --> 00:07:44,310 we enumerate over it. 111 00:07:44,310 --> 00:07:46,680 This is called deferred execution. 112 00:07:46,680 --> 00:07:50,750 The statement isn't evaluated until we iterate through it with a for each or 113 00:07:50,750 --> 00:07:54,680 use a method on it like count that forces it to be evaluated. 114 00:07:54,680 --> 00:07:55,305 So let's do that. 115 00:07:55,305 --> 00:08:03,579 NumbersGreaterThanTen.Count(). 116 00:08:03,579 --> 00:08:05,048 But when we're here in the REPL, 117 00:08:05,048 --> 00:08:07,950 our statements are automatically being evaluated. 118 00:08:07,950 --> 00:08:11,720 We'll see some more examples of deferred execution later on in this course.