What Is LINQ And Why Do I Want To Use It?

LINQ or Language Integrate Query is a component of Visual Studio developed by Microsoft and introduced in 2007. It simplifies the creation and execution of query expressions. It can be used with either the C# or the Visual Basic programming languages.

LINQ contains tools and methods for easy querying of data sets which contain different types. For example, if you wanted to find out how many red cars are contained in a list of cars, it will allow you to quickly find that out.

It can also help you with more complicated queries such as finding customers in a certain state, that have bought a certain product, from a certain store. Without LINQ, you would have to iterate over the set with a series of nested and complicated for each loops.The syntax that LINQ uses is easier to create, read and maintain.

LINQ can also help when your program needs to interface with an outside data source. These data sources can be SQL databases, XML documents and other web services. Each of these has their own queries and syntax that you can use to interface with their data. With this, LINQ provides a way to retrieve data from these sources into a .NET application making development much easier.

Instead of learning a new syntax to query from each different source, you can use a LINQ extension for each new source. If you are interfacing with data sets in C#, LINQ is a great tool to help you get the job done.

There are two flavors of LINQ. The query syntax (looks like SQL):

var res = from p in ppl
        where p.Name == "Fred"
        order by p.Age
        select p.Name;

and the method syntax (methods with lambda expressions).

var res = 
  ppl.Where(p => p.Name == "Fred")
	.OrderBy(p => p.Age)
	.Select(p => p.Name);

You can use either, but in my experience the method syntax is more intuitive and more popular.

I normaly use the extension methods for every day use. The query syntax, I reserve mostly for joins, aggregates and anonymous class returns because it's easier to read and understand in those cases. 

Using LINQ can save you on writing some complicated code. Here is a short example of C# code which will be much simplified using LINQ. The purpose is to find all the even numbers in a list of numbers. First the pure C# version of the code:

List<int> Evens(List<int> numbers) 
  var evens = new List<int>(); 
  for(var i=0; i<numbers.Length; i++) 
    if(numbers[i] % 2 == 0)
  return evens; 

Now this code represented with LINQ will look like this:

numbers.Where(n => n%2 == 0).ToList();

You can see that the LINQ version is much more readable and shorter. One more thing to note is that at the end of the last line of code, I'm calling the method .ToList() which immediately executes the query and puts the results into a list. If I didn't use a .ToList() method, then the query would've been Lazy Loaded and it wouldn't execute until a .ToList() is called on it which can be done later in the code after some other code is executed.

LINQ is a very powerful tool which has many features that I didn't cover in this article but I hope this brief introduction was helpful to you. 

Published:   4/16/2017