This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn about LINQ providers and get some tips on making your queries faster.
LINQ Providers
Practice Projects with LINQ
- Refactor your code: Take code you've written and change the
for
andforeach
loops into LINQ queries. - Refactor someone else's code: Find an open sourced project on Github, fork it, and see if you can refactor it with LINQ queries.
- Rock, Paper, Scissors: Create a Rock, Paper, Scissors game in a console application that keeps track of wins and losses. Report on win/lose/draw statistics after each game is played. Attempt to predict which option the player will choose to try and outsmart them!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that you've got the power of LINQ in
your skill set, it's time to go out and
0:00
query all the things.
0:04
What we've been using in this course
is referred to as LINQ to Objects
0:07
which means that we've been using
LINQ on in-memory collections.
0:11
But you can also use LINQ
with remote data sources, or
0:15
data that doesn't exist within your
program, by the use of LINQ providers.
0:19
When you see the phrase LINQ to, it
usually means that it's a LINQ provider.
0:24
A LINQ provider is a piece
of software that enables
0:29
the use of LINQ to query
an external datasource.
0:32
Some popular providers are LINQ to SQL,
LINQ to XML,
0:36
and there's even a LINQ to Twitter.
0:40
LINQ to Entities is a provider
that's used with Entity Framework,
0:42
which you'll learn about
here at Treehouse too.
0:46
I've put a list of some common
providers in the teacher's notes for
0:49
you to check out.
0:52
These providers implement an interface
that we haven't covered here called
0:54
IQueryable.
0:58
IQueryable inherits from IEnumerable,
and it lets us use all the LINQ
0:59
methods that we've covered in this
course You can usually use Iquerable is
1:04
just as you would with IEnumerable but
there's one thing to keep in mind.
1:08
Remember when we talked about
deferred execution with,
1:13
LINQ providers you're working
with the remote data source.
1:16
So it's important to keep in mind
when your query actually executes.
1:20
You can build a giant query with multiple
WHERE clauses and joins and sorting but
1:24
nothing actually happens until
you iterate over the result.
1:29
Sometimes you'll write a query and it will
be super slow as you get more familiar
1:34
with LINQ there are some strategies that
you can use to make your queries faster.
1:39
Certain LINQ operators execute immediately
aggregate operators like sum and
1:44
count have to iterate over everything
in the collection in order to evaluate.
1:49
Is your query executing before it has to?
1:54
Does it fetch data from
the remote source multiple times?
1:57
How many times does it iterate over
a collection, and over how many elements?
2:01
If you search Stack Overflow for
the terms slow LINQ query.
2:05
You'll see a ton of people asking for
advice on their queries.
2:10
Don't hesitate to ask for help.
2:14
Sometimes the answer stares
you right in the face, but
2:16
it takes another set of
eyes to actually see it.
2:18
The Treehouse community is
a great place to ask for
2:20
help another thing I like to do
is break the query apart and
2:24
time it like we did with comparing
union and can cat operators.
2:28
Also keep in mind how much
data you're returning.
2:32
Make use of projection
with the select operator
2:36
to create anonymously typed objects
that contain only what you need.
2:38
I love LINQ because it lets me do so
much with fewer lines of code.
2:44
The next time you start to write a for
2:49
each loop, think to yourself,
can I use LINQ for this?
2:50
The more you practice writing LINQ
queries, the more natural it will feel.
2:54
Do you already have some projects
where you can practice using LINQ?
3:00
If not check the teachers notes for
3:03
some ideas, we'll be using LINQ
often in the courses that follow.
3:05
Especially when we get into using
databases in our applications
3:09
are you excited.
3:12
I know I am.
3:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up