This is part one of a series of blog posts on LINQ. I’m writing it because I need to remind myself how to use LINQ well, and because, while LINQ was covered extensively when it was first introduced, it has been a bit neglected lately.
Step 1 in learning LINQ is to get the right programming editor. I can make a case for Visual Studio, for Sublime, for many editors, but for learning LINQ there really is only one good choice: LinqPad. This brilliant tool is great for learning LINQ and for working in LINQ and is worth its weight in gold. I’d happily spend $100 on a tool this good, but the basic LINQPad is free. There is a Pro and a Premium version. If you program for a living, do not hesitate, buy the Premium edition which is currently priced at $58 and is normally $75, and well worth it.
There are quite a few good books available on LINQ. I have a book called Programming Reactive Extensions and LINQ which I’ll be drawing on for this series. Another excellent source is C# 5 In A Nutshell by Joseph and Ben Albahari. Not only are they two of the nicest guys you can hope to meet, they are brilliant, and Joseph is the author of LINQPad. You can’t go wrong.
|Note: This series assumes you are already familiar with the fundamentals of C#. If not, please consider my Pluralsight course: C# From Scratch.|
LINQ stands for Language Integrated Query and is pronounced “link.” LINQ provides a syntax very similar to SQL within C# for querying data. One big win is that LINQ provides a more functional approach to C# than had been available previously.
To get started, let’s write a LINQ program in LINQPad. To get started, set the language in LINQPad to Statements, as shown in the figure.
As you can see, you have quite a few choices. We’ll be working in C#.
Next, we’ll create a list of integers in memory and then query that list using LINQ, selecting only the integers that match our criteria,
var query = from x in integers
where x % 2 != 0
foreach (var val in query)
This code is in three parts. In the first part, we create a new list of integers, with the values 1-16
In the second part, we execute our LINQ query. Notice that it begins with from and ends with a select statement. This is the standard form of a LINQ query.
The third part iterates over the results returned from the LINQ query and causes a display of the odd numbers 1-15.
.Where(x => x % 2 != 0);
This will have the same effect as the original query statement. It uses a Lambda expression (x => x % 2 != 0) which will be explained in detail in an upcoming article.
The first form we used is called Query Syntax. The second is called Fluent Syntax. Many people prefer one form over the other, though in fact in most cases it is really just a matter of taste.
Upcoming articles will cover Lambda expressions in more detail as well as various LINQ operators. We’ll then go on to tackle some of the more important LINQ operators and end up with discussions of LINQ to SQL.
Latest posts by Falafel Posts (see all)
- Matching Complex Query String Rewrite Rule in IIS - March 22, 2017
- Using Google Services in UWP C# Apps – Part 2 - February 7, 2017
- Using Google Services in UWP C# Apps – Part 1 - February 6, 2017
- Redis Caching in the Google Cloud Platform - February 3, 2017
- Entity Framework with Google Cloud SQL - February 2, 2017