If you’re coming to Windows programming and C# from the world of iOS, you’ve probably heard tell of LINQ but aren’t quite sure what it is. Or maybe that’s just me. In any case, if you’re interested, here’s a quick primer on LINQ and how it might be of use.

What is it?

Imagine you have designed the data structure for your app. It’s pretty complicated, with an array of arrays and various strings and status fields. It looks logical, but you know that when it comes to examining the data to perform specific actions, you’re going to end up with a horrific nested set of loops and maybe a page of switch/case or if/then statements.

This is what LINQ is great at simplifying. LINQ allows you to write a query - a set of rules - which will look at your data, and construct a list containing only the items that match your needs. For example, if you have an array of people with fields for ages, gender, hair styles, you can write one query to search for greying men in their 40s, and hey presto! you get a list that includes me.You can then iterate over this list, and display the names / ban them from your dating site or whatever you want to do with it.

 The point is that the query will replace any potentially horrific nested set of if/then statements and greatly simplify your code.

Show me!

Here's an example, which pretty much follows on from a blog posting a few days ago on databinding to ListBoxes.

Let's say you are constructing an app that displays a series of website bookmarks. You need to store the URL of the link of course, and a description. However, you decide that it would also be nice to store a series of boolean tags to provide a little more information. Specifically, you want to define if the link is from a US website, or a UK or French one. You also want to store details on what it links to: is it news, sports, business or weather. Here's the data structure I used to achieve this:

 

Each weblink object stores the name and URL, but also the tag fields.

Here's the code which declares a couple of these links, including setting the tag values:

 

In this app, setting the tag values is a manual process: this is a curated list after all. But now the fun part: Imagine that the app user is only interested in websites from the UK that are about news. To construct this list from the data, you would have to loop through all the category objects, loop through all the links, and perform an if/then on the tag fields to find the ones that are from the UK and about news. Then you would have to store these links in another List<> object, and keep track of that. If you had to repeat this for a few different scenarios, there is no doubt you would get very bored writing a lot of loops and ifs and thens.

So say hello to your new best friend: LINQ. By constructing a query object which does all the looking in objects for you (including nested arrays) and which then lets you add some conditions, you can use something like this instead:

This command does all that looping for checking for you. After it executes, the variable query contains a list of objects which match the description (or it's empty), and you can then use it to display the links you want. Or just display them at the console, like this, using a familiar foreach loop:

 

LINQ is a very powerful feature which is going to save you a lot of typing boring code. For more information, there's some good content on MSDN including Introduction to LINQ Queries (C#), which goes into a lot more detail.