Friday, May 16, 2014

LINQ - An Introduction

Intro

LINQ stands for Language Integrated Query. It was introduced with Visual Studio 2008, but it is probably still a new concept for many so I figured it just might be worth a blog on the topic. To me, LINQ is basically an extension of .Net that lets you use sql-like query syntax to retrieve some types of data. Let's say for example you have a list of customers and you want to retrieve all customers who have blue hair. If you were to do this in sql, it might look something like "select * from tblCustomers where HairColor = 'blue'". In C# you'd have to create a temp list, then write a loop where you find all blue-hairs and insert them into your temp list. It's just more work. LINQ however lets you treat some aspects of .Net as though you were using something akin to SQL.

On top of that, LINQ lets you pull data from many different types of sources. You can use LINQ to SQL to use LINQ for querying databases, you can use LINQ to XML to use link for querying XML files, and of course there's just plain old LINQ for querying lists of objects in .Net. By learning LINQ, you learn how to, in some fashion, query many different types of data so it saves you a little bit of code and a little bit of extra brainpower for learning other new things. Neat huh?

Constructing a LINQ Query

A LINQ query is comprised of 3 clauses: from, where, and select. If you're familiar with SQL syntax you'll notice that this isn't quite the same. In SQL, it's select, then from, then where. But hey, if this were the exact same order then life wouldn't be fun at all, and hippos would rule the seas. Or something. The from clause is your data source, the where clause is your filtering, and the select clause is what you're returning. Let's see a quick sample:

        protected void btnLinq1_Click(object sender, EventArgs e)
        {
            var blurbs = new string[] { "fred", "george", "fred rocks", "george doesn't" };
            var query = from blurb in blurbs
                        where blurb.StartsWith("fred")
                        select blurb;
            foreach (var val in query)
                Response.Write(" " + val);
        }


The above sample is a button click event in a webforms project. We first define our data source, which in this case is a simple array of strings named blurbs. We then construct our query object. Our query has a from clause that shows we are pulling data from blurbs, the where clause states that we only want to retrieve those blurbs who start with the word "fred", and the select clause states we are selecting individual blurbs from the list of blurbs. The last 2 lines loop through our query results (tip: the query isn't actually run until its results are used! see Introduction to LINQ Queries for more info) and write them out to the web page. As you can guess, we only retrieve and thus only display the values "fred" and "fred rocks" from the list of blurbs.

What's Next?

Either wait until next week's blog post when I delve a little deeper, or be a suck-up and read some of the resources below for more info.

Resources

LINQ
Introduction to LINQ
Introduction to LINQ Queries
Basic LINQ Query Operations

No comments:

Post a Comment