All About LINQ3:20 with Carling Kirk
What is LINQ and how can we use it?
Hi I'm Carling, I'm a Treehouse teacher and a developer. 0:05 In this course, I'm going to teach you all about LINQ. 0:09 LINQ is a powerful toolset in the .NET Framework 0:12 that enables C# developers to query and manipulate collections of objects. 0:15 LINQ stands for language integrated query. 0:20 LINQ was introduced in C# 3.0 to give developers the querying power of 0:24 a database querying language like SQL. 0:28 If you've used SQL, or S-Q-L, LINQ will seem pretty familiar. 0:32 But if you haven't, no worries. 0:36 You can still learn to use LINQ with this course. 0:39 LINQ doesn't really provide much we couldn't do before, so 0:42 then why would we use it? 0:45 Why reinvent the wheel? 0:47 Well, it makes it way easier and 0:48 faster to write code that would otherwise be verbose and complicated to read. 0:50 Before LINQ if we wanted to get a subset of objects from a collection, 0:56 we would write a loop, compare each object to see if it was the one we want and 1:00 then stick it into a new collection. 1:05 It could end up being 20 lines long. 1:07 Now with LINQ, we can do it with only one line of code. 1:10 And even better, our code becomes more readable and 1:13 self-documenting because the methods we'll use like Where or 1:17 OrderBy are more like writing a sentence than actual code. 1:21 Along with LINQ came a lot of other new language features to help support it. 1:26 We'll touch on all these along the way as we learn to query with LINQ, but 1:30 you can find a list of them in the teacher's notes. 1:34 So how do we use LINQ? 1:38 We can use LINQ on collections like arrays or lists. 1:40 If you haven't heard of a list or a collection yet, you should check out 1:44 the teacher's notes for more resources before diving into LINQ. 1:48 But more specifically we can use LINQ with anything that implements 1:51 IEnumerable of T in the system.collections.genericnamespace. 1:57 IEnumerable is an interface in the .NET framework that, when implemented for 2:02 a type, tells us that it has an enumerator. 2:07 That means that the objects in it can be looped through like we do in a for or 2:11 a for each loop. 2:15 If all this sounds unfamiliar to you, 2:18 you should check out our course on C# objects to learn about arrays and loops. 2:20 You can find a link in the teacher's notes. 2:25 I'll be using the terms collections and 2:28 sequences interchangeably throughout this course. 2:30 Just keep in mind that if I use either term, it's really anything that implements 2:33 IEnumerable or, more generally, anything that we can use LINQ with. 2:38 There are two flavors of syntax we can use when writing LINQ queries, 2:44 query syntax and method syntax. 2:48 Every developer has their preference, but there are some 2:51 things you can do with method syntax that you can't do with query syntax. 2:54 We're going to start out by using query syntax, but since you'll probably be 2:58 reading both in the future, we'll get into using method syntax a little later. 3:03 Using method syntax requires getting into those supporting language features I 3:08 mentioned earlier. 3:12 All right, my fingers are itching to code. 3:15 Let's open up Workspaces and start playing with LINQ. 3:17
You need to sign up for Treehouse in order to download course files.Sign up