Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Quantifiers5:42 with Carling Kirk
Learn about the LINQ quantifier operators, Any, All, and Contains.
Now that we've got the most commonly used query operators down. 0:04 Where, order by, and select. 0:08 It's time to dive and 0:10 explore the rest of the query operators that Lync has to offer. 0:11 These next operators allow us to perform more complex queries on our data. 0:15 Some of the operators to follow are more common than others. 0:20 I have to check out the documentation for Lync all the time. 0:25 It's not your job as a developer to memorize all these operators, but 0:29 know they exist and when to use them to solve a problem. 0:33 Let's take a quick look at an overview article to see what I mean. 0:38 Here we've got the categories of all the operators we'll be going over. 0:42 The first type of operators we'll be covering is quantifiers. 0:46 We can use quantifiers to determine if a sequence or 0:50 collection has objects that meet certain criteria. 0:53 It's a little bit like the where operator, but instead of returning the objects that 0:57 meet the criteria, the quantifiers return a boolean value like true or false. 1:01 There are three quantifier operators in Lync. 1:07 Any, all and contains. 1:10 Any and contains checks to see if at least one object matches, and 1:14 all checks to see if all the objects match. 1:19 The any operator is sometimes used to check if something exists 1:22 before doing some kind of operation like adding an object to a collection. 1:26 First, we need to load up our birds again in workspaces. 1:31 You know the drill, C#. 1:37 LoadAssembly("BirdWatcher.dll"). 1:40 Then, using BirdWatcher. 1:50 And var birds = BirdRepository.LoadBirds(). 1:55 Okay. 2:05 So if we were about to try and add a new bird to the BirdWatcher data, we might 2:06 wanna check if there exists beforehand so we wouldn't have a duplicate. 2:10 We can use the any operator to see if any birds exist with the same name. 2:15 We'll need to pass it a predicate delegate just like the where method. 2:19 BIrds.Any where b 2:23 goes to b.Name equals Crow. 2:28 True. 2:37 So, if the bird's list has a bird object with the name crow, it returns true. 2:37 We should only add a crow if one doesn't exist, so we would use it like this. 2:43 If there are no birds where any of 2:48 the birds goes to b.Name equals 2:52 Crow Then 2:57 birds.add, new bird where 3:04 Name equals Crow, and close. 3:10 So we should have only one crow. 3:17 Yep. 3:21 You can also use any without an argument, 3:23 to see if there's anything at all in the collection, birds.any. 3:25 What do you think this will return? 3:29 True. We've got all about seven birds. 3:32 Now, contains is similar to any, but instead of using a predicate delegate, 3:35 we pass an object to the method to check against. 3:41 Let's create a sparrow, 3:44 new Bird where the Name = Sparrow and 3:49 its Color is Brown. 3:56 So, we would use it like, if there are no birds, where Contains a sparrow. 4:00 Then, let's see if I can use the up arrow to get my birds.Add. 4:11 There we go. 4:17 We would add a Sparrow and the Color should be Brown. 4:19 So now, we should have a sparrow at the end. 4:32 On the flip side, if we will need to make sure all the objects in a collection 4:37 meet a condition, we'd use all. 4:40 Behind the scenes it's, the exact opposite of any. 4:43 So we could write our previous check for a bird named sparrow with the all operator. 4:46 Birds.All, where b goes to 4:52 b.Name is not equal to Sparrow. 4:57 And that's false because there's one that is a sparrow. 5:02 But which one would be faster? 5:05 It would be dependent on how many objects are in the collection, and 5:07 how likely the condition would pass or fail. 5:10 When any is called on a collection, it evaluates the predicate on 5:14 each of the elements until one of them passes, and then it stops evaluating. 5:17 This is called short circuit evaluation. 5:22 And with the all operator, as soon as it finds an element that doesn't fit 5:25 the predicate it stops evaluating. 5:30 So, if it's more likely that the condition would prove true early on, 5:32 it's good to use any. 5:36 But if it's more likely that the condition would prove false, use all. 5:37
You need to sign up for Treehouse in order to download course files.Sign up