1 00:00:00,250 --> 00:00:04,170 Up to this point, we've been retrieving entities in their implicit or 2 00:00:04,170 --> 00:00:05,530 natural order. 3 00:00:05,530 --> 00:00:09,520 This can cause the entities to be displayed in unexpected ways. 4 00:00:09,520 --> 00:00:12,190 Because of this, it's typically a requirement for 5 00:00:12,190 --> 00:00:16,070 results to be explicitly sorted by one or more properties. 6 00:00:16,070 --> 00:00:20,390 We can explicitly sort the results of a query using the OrderBy or 7 00:00:20,390 --> 00:00:23,570 OrderByDescending link operators. 8 00:00:23,570 --> 00:00:26,230 Let's start with removing the Where link operator so 9 00:00:26,230 --> 00:00:29,490 our query will retrieve all of the comic books from the database. 10 00:00:31,408 --> 00:00:36,056 Then, let's sort the results of our query by issue number, in descending order. 11 00:00:36,056 --> 00:00:43,338 .OrderByDescending(cb => cb.IssueNumber). 12 00:00:49,757 --> 00:00:54,163 Here's the list of our comic books sorted in descending order by issue number. 13 00:01:00,870 --> 00:01:04,210 Now our query contains a SQL ORDER BY clause. 14 00:01:05,710 --> 00:01:10,189 It's important to note that you can't add multiple calls to the OrderBy or 15 00:01:10,189 --> 00:01:14,398 OrderByDescending operators like we could with the Where operator. 16 00:01:16,593 --> 00:01:23,648 .OrderBy(cb => cb.PublishedOn). 17 00:01:23,648 --> 00:01:26,950 The last call made will be the one that is used. 18 00:01:26,950 --> 00:01:31,164 If you need to sort on more than one column, you can use the ThenBy or 19 00:01:31,164 --> 00:01:33,206 ThenByDescending operators. 20 00:01:36,631 --> 00:01:39,460 TheBy. 21 00:01:39,460 --> 00:01:44,122 Now our query will be sorted by the IssueNumber column values in descending 22 00:01:44,122 --> 00:01:48,350 order, then by the PublishedOn column values in ascending order. 23 00:01:52,688 --> 00:01:56,880 Let's look at an example of how we can leverage deferred execution to extend 24 00:01:56,880 --> 00:01:58,350 a query. 25 00:01:58,350 --> 00:02:00,670 We can start by defining our base query. 26 00:02:00,670 --> 00:02:03,258 That includes a descending sort on issue number. 27 00:02:03,258 --> 00:02:10,132 var comicBooksQuery = context.ComicBooks.Include, 28 00:02:10,132 --> 00:02:16,460 so our series navigation property will be populated. 29 00:02:17,520 --> 00:02:20,553 (cb => cb.Series). 30 00:02:20,553 --> 00:02:29,030 Then .OrderByDescending(cb => cb.IssueNumber). 31 00:02:29,030 --> 00:02:33,386 Then we could execute that query to retrieve a list of all of the comic books. 32 00:02:33,386 --> 00:02:41,170 var comicBooks = comicBooksQuery.ToList(). 33 00:02:41,170 --> 00:02:45,800 We could also add a Where operator to the base query and execute that query 34 00:02:45,800 --> 00:02:49,730 to retrieve the comic books that have an average rating less than seven. 35 00:02:49,730 --> 00:02:53,438 var comicBooks2 = 36 00:02:53,438 --> 00:02:59,983 comicBooksQuery.Where(cb => 37 00:02:59,983 --> 00:03:08,061 cb.AverageRating < 7).ToList(). 38 00:03:11,163 --> 00:03:15,241 Because the base query includes an OrderByDescending operator, 39 00:03:15,241 --> 00:03:18,660 both queries will include an OrderBy clause. 40 00:03:18,660 --> 00:03:23,125 Before we test our queries, let's add a second foreach loop to output the second 41 00:03:23,125 --> 00:03:25,111 list of comic books to the Console. 42 00:03:27,985 --> 00:03:34,716 Foreach (var comicBook in comicBooks2), 43 00:03:34,716 --> 00:03:43,352 then Console.WriteLine(comicBook.DisplayText). 44 00:03:44,430 --> 00:03:48,456 Let's also write the count of the second comic books collection to the Console.. 45 00:03:54,140 --> 00:03:58,443 And let's output an extra blank line just above the second list. 46 00:04:06,355 --> 00:04:09,290 And here's our two lists of comic books. 47 00:04:09,290 --> 00:04:13,410 The first list is a complete list of comic books sorted by issue number 48 00:04:13,410 --> 00:04:14,930 in descending order. 49 00:04:14,930 --> 00:04:18,680 The second list is a list of the comic books that have an average rating less 50 00:04:18,680 --> 00:04:19,800 than seven. 51 00:04:19,800 --> 00:04:22,771 Also sorted by issue number in descending order. 52 00:04:30,240 --> 00:04:33,639 Here's the first query that retrieves all of the comic books. 53 00:04:38,050 --> 00:04:40,385 Notice that it has an ORDER BY clause. 54 00:04:45,110 --> 00:04:48,877 And here's the second query that is filtering by the average rating. 55 00:04:52,897 --> 00:04:55,930 As expected, it also has an ORDER BY clause.