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