LINQ From Scratch

By January 30, 2014Uncategorized

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 shutterstock_138083054LINQ was covered extensively when it was first introduced, it has been a bit neglected lately.

LinqPad

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.

First Steps

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 SQLStatements 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 integers = new List<int>()
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

var query = from x in integers
where x % 2 != 0
select x;

foreach (var val in query)
Console.WriteLine(val);

 

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.

Lambda Expressions

var query = integers
.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.

Next Steps

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.

Thanks.

The following two tabs change content below.