LINQ for the Beginner

Introduction

The purpose of this paper is to present some basics about LINQ for certain individuals who may have not gained an understanding of LINQ. LINQ unifies data access, whatever the source of data, and allows mixing data from different kinds of sources. LINQ means "Language-Integrated Query". It allows for query and set operations, similar to what SQL statements offer for databases. LINQ, though, integrates queries directly within .NET languages like C# and Visual Basic through a set of extensions to these languages.

Before LINQ, developers had to juggle with different languages like SQL, XML or XPath, and various technologies and APIs like ADO.NET or System.Xml, in every application written using general-purpose languages like C# or VB.NET. It goes without saying that this had several drawbacks. LINQ kind of welds several worlds together. It helps us avoid the bumps we would usually find on the road from one world to another: using XML with objects, mixing relational data with XML, are some of the tasks that LINQ will simplify. One of the key aspects of LINQ is that it was designed to be used against any type of object or data source, and provides a consistent programming model for doing this. The syntax and concepts are the same across all of its uses: once you learn how to use LINQ against an array or a collection, you also know most of the concepts needed to take advantage of LINQ with a database or an XML file. Another important aspect of LINQ is that when you use it, you work in a strongly-typed world. Examine this basic code and see if it shows any link to a data source:

The code declares a method Square to then declare an implicitly-typed local variable to perform that said operation on an array, or sequence, of three integers. The select method emits a sequence where each input element is transformed within a given lambda expression. The iteration of each element enables the operation to be performed on each element. As a matter of fact, the general idea behind an enumerator is that of a type whose sole purpose is to advance through and read another collection's contents. Enumerators do not provide write capabilities. This type can be viewed as a cursor that advances over each individual element in a collection, one at a time. IEnumerable<t> represents a type whose contents can be enumerated, while IEnumerator<t> is the type responsible for performing the actual enumeration. The basics units of data in LINQ are sequences and elements. A sequence is any object that implements the generic IEnumerable interface, and an element is each item in the sequence. Here is a basic code example:

Now we compile our ObjectDumper.cs file into a DLL by using the /target:library switch on the command-line, or we compile it as a class file on VS 2010. Note that if you are using VS 2010, be sure to go to the Project's properties and ensure that the .NET platform is 4.0. Now we compile the above file, MyProgram.cs, with a reference to ObjectDumper.dll: csc.exe /r:ObjectDumper.dll MyProgram.cs. Here is the output:

Share

About the Author

I started electronics training at age 33. I began studying microprocessor technology in an RF communications oriented program. I am 43 years old now. I have studied C code, opcode (mainly x86 and AT+T) for around 3 years in order to learn how to recognize viral code and the use of procedural languages. I am currently learning C# and the other virtual runtime system languages. I guess I started with the egg rather than the chicken. My past work would indicate that my primary strength is in applied mathematics.

Hi!
This is interesting. I have (by hand, in order to learn better) copied the code of the process / memory list application and tried to compile it. However, it chokes om the following line:var processes = new List();
The class List() is apparently not found. It can be replaced with a generics list with a specified type, but then it complains about not finding the "Memory" and "TotalMemory" methods of the list object.

I did not place the data type <string> to IEnumerable. I wrote the article in HTML mode and did not realize that words, like include <iostream>, <string>, etc, must be typed in manuallyoutside of HTML mode in the article submission wizard. Thank you for catching that.