1. Computing

LINQ - A C# Tutorial


1 of 4

Introduction to LINQ in C#

This tutorial is the first of several on LINQ. But before we can move on to LINQ, we need to learn some new features of C# that you may or may not be aware of. These are anonymous types and functions, extension methods, and lambda expressions. Most of these were introduced with C# 3.0. These are used extensively in LINQ so it's important to know them:

LINQ, introduced in late 2007 is a way of querying data in arrays, lists, enumerable classes, XML, relational databases and anything built on these. It has a bit of a learning curve and there are aspects of it that aren't at first obvious such as Lambda Expressions but once you understand them, it will make your software more powerful.

Anonymous Types

You are probably used to declaring variables with a simple type such as int or string or a class you've created; and with a name. Here's an example of an anonymous type in action. Create a new console project and paste these three lines in so the Main looks like this:

static void Main(string[] args)
   var me = new {Name = "David", Age = 52};
   Console.WriteLine("Name = {0}, Age = {1}",me.Name,me.Age) ;
   Console.ReadKey() ;

This works as expected, printing out Name = David, Age = 52. It's unusual that the type of me is unknown (ie anonymous) and the two properties, Name and Age are public and read only. There are limits:

  • No methods or events in the class.
  • Properties are public and read-only
  • Values assigned to the properties cannot be null, an anonymous function, or a pointer type.

Not much use you'd think but we'll see them used extensively in LINQ typically to select variables as the results of LINQ queries. Think of them as exposed temporaries.

Duck Typing

If two types have the same number and types of properties then those two types are considered the same. This is called Duck Typing. "If it walks like a duck and swims like a duck and quacks like a duck then it can be considered to be a duck". See Wikipedia on Duck Typing.

Familiarity with foreach

If you are used to using a simple for loop for processing a list or array then you should find foreach useful. To use foreach, all you needs is a list of something that is enumerable, ie it implements the methods defined in the IEnumerable interface. Virtually all C# collections are enumerable, as are arrays. Here is an example of using foreach working with an array:

static void Main(string[] args)
   var names = new string[]{"Alpa","Bertie","Charlie"};
   foreach (var name in names)
      Console.WriteLine("Name = {0}",name) ;
   Console.ReadKey() ;

That prints out the three names in the names array. Note that as with the previous example I've used the var keyword to declare the variables. Earlier versions of C# may need the types explicitly declared.

On the next page:Extension Methods

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. Introduction to LINQ in C#

©2014 About.com. All rights reserved.